oscarotero / simple-crud

PHP library to provide magic CRUD in MySQL/Sqlite databases with zero configuration
MIT License
242 stars 58 forks source link

Can't insert image #18

Closed elieobeid7 closed 7 years ago

elieobeid7 commented 7 years ago

I can't add it to array like so

https://stackoverflow.com/questions/46851647/cannot-insert-input-type-file-to-array-of-html-names

so I had to

<input type="file" name="photo" id="photo" class="custom-file-input" accept="image/*"/>

and then process it using another library to resize it and so on and finally I do

if (isset($_POST['submit'])){    
    foreach ($data as &$record) {
        $record['photo'] = $photo;
        }        
 $db->user->insert()
    ->data($data)
    ->run(); 

The error

Fatal error: Uncaught SimpleCrud\SimpleCrudException: Invalid fields in /opt/lampp/htdocs/web/diaspora/common/vendor/simple-crud/simple-crud/src/Table.php:453 Stack trace: #0 /opt/lampp/htdocs/web/diaspora/common/vendor/simple-crud/simple-crud/src/Queries/Mysql/Insert.php(26): SimpleCrud\Table->prepareDataToDatabase(Array, true) #1 /opt/lampp/htdocs/web/diaspora/index_ctrl.php(37): SimpleCrud\Queries\Mysql\Insert->data(Array) #2 {main} thrown in /opt/lampp/htdocs/web/diaspora/common/vendor/simple-crud/simple-crud/src/Table.php on line 453

And it's all because of the photo. If I don't use $data array all goes smoothly as discussed previously but I have too many fields, I'd rather use array or pass $photo alongside $data somehow

oscarotero commented 7 years ago

It's not recomended save images directly in a database, and this library does not support it. The recomended way to handle a file is saving it in the filesystem and store the file path in the database.

If you use PSR-7 (I suspect you don't), you could use the File extra-field (https://github.com/oscarotero/simple-crud-extra-fields#file)

If not, you can do it manually:

$photo = '/path/to/image.jpg';
move_uploaded_file('photo', __DIR__.$photo);
$data['photo'] = $photo;
$db->user->insert()->data($data)->run();
elieobeid7 commented 7 years ago

but I'm not saving the actual file, I'm just saving its name and extension, here's the full script

require "common/vendor/samayo/bulletproof/src/utils/func.image-resize.php";
$image = new Bulletproof\Image($_FILES);

$data = array($_POST['data']);
$cdata = array($_POST['cdata']);

$image->setMime(array('jpeg', 'gif', 'png', 'jpg'))
->setLocation(__DIR__ . "/avatars");

if($image["photo"]){  
  $upload = $image->upload(); 
  if($upload){
      $resize = Bulletproof\resize(
          $image->getFullPath(), 
          $image->getMime(),
          $image->getWidth(),
          $image->getHeight(),
          128,
          128
   );
$photo = $upload->getName() . '.' . $upload->getMime();
  }else{
      $photo = NULL;
  }
} 
if (isset($_POST['submit'])){    
    foreach ($data as &$record) {
        $record['photo'] = $photo;
        }        
 $db->user->insert()
    ->data($data)
    ->run(); 

}

using this library

https://github.com/samayo/bulletproof

oscarotero commented 7 years ago

Does photo field exist in the database?

elieobeid7 commented 7 years ago

yes varchar(255) | utf8_unicode_ci

only when inserting as array i face this problem

oscarotero commented 7 years ago

This error occurs on insert a key that does not exists in the database scheme. Maybe you have the scheme cached and it's not updated with this new field? If the field is loaded by simple-crud, you can access to it by $db->user->photo

elieobeid7 commented 7 years ago

yes i guess something like that, now it works thanks