WebbieWorks's avatar

Multiple Images not storing into database

I am fairly new to laravel and I am stuck on an issue with multiple images getting stored. The print_r($data) shows that the info is there, but they are not being stored. The 1st image uploads without issue, but not the others.

[image] => Illuminate\Http\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Image-1.jpg
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
            [hashName:protected] => 
            [pathName:SplFileInfo:private] => /Applications/MAMP/tmp/php/phpMwY6SH
            [fileName:SplFileInfo:private] => phpMwY6SH
        )

[images] => Array
        (
            [0] => Illuminate\Http\UploadedFile Object
                (
                    [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                    [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Image-2.jpg
                    [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
                    [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                    [hashName:protected] => 
                    [pathName:SplFileInfo:private] => /Applications/MAMP/tmp/php/phpWbVdIX
                    [fileName:SplFileInfo:private] => phpWbVdIX
                )

            [1] => Illuminate\Http\UploadedFile Object
                (
                    [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                    [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Image-3.jpg
                    [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
                    [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                    [hashName:protected] => 
                    [pathName:SplFileInfo:private] => /Applications/MAMP/tmp/php/php46m8RX
                    [fileName:SplFileInfo:private] => php46m8RX
                )

        )

But It is not being sent to db

My controller looks like it is correct based off a tutorial I followed.

if($request->hasFile('image')){
                $image_tmp = Input::file('image');
                if($image_tmp->isValid()){
                    $extension = $image_tmp->getClientOriginalExtension();
                    $filename = rand(111,9999).'.'.$extension;
                    $large_image_path = 'images/backend_img/products/large/'.$filename;
                    $medium_image_path = 'images/backend_img/products/medium/'.$filename;
                    $small_image_path = 'images/backend_img/products/small/'.$filename;
                    //Resize Images
                    Image::make($image_tmp)->save($large_image_path);
                    Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
                    Image::make($image_tmp)->resize(300,300)->save($small_image_path);
                    $product->image = $filename;
                }
            }
if($request->hasFile('images[]')){
                $image_tmp = Input::file('images[]');
                if($image_tmp->isValid()){
                    $extension = $image_tmp->getClientOriginalExtension();
                    $filename = rand(111,9999).'.'.$extension;
                    $large_image_path = 'images/backend_img/products/large/'.$filename;
                    $medium_image_path = 'images/backend_img/products/medium/'.$filename;
                    $small_image_path = 'images/backend_img/products/small/'.$filename;
                    //Resize Images
                    Image::make($image_tmp)->save($large_image_path);
                    Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
                    Image::make($image_tmp)->resize(300,300)->save($small_image_path);
                    $product->images = $filename;
                }
            }
            

as well as my input field

<div class="control-group">
   <label class="control-label">Gallery Images :</label>
      <div class="controls">
          <input type="file" name="images[]" id="images" multiple />
     </div>
</div>

Any help would be appreciated it on this issue.

0 likes
5 replies
Sergiu17's avatar

You don't need to check two times if request has file.

if ( $request->has('images') ) {
    $files = $request->file('images');

    foreach ( $files as $file ) {
        if ( $file->isValide() {
            $extension = $image_tmp->getClientOriginalExtension();
            $filename = rand(111,9999).'.'.$extension;
            $large_image_path = 'images/backend_img/products/large/'.$filename;
            $medium_image_path = 'images/backend_img/products/medium/'.$filename;
            $small_image_path = 'images/backend_img/products/small/'.$filename;
                    //Resize Images
            Image::make($image_tmp)->save($large_image_path);
            Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
            Image::make($image_tmp)->resize(300,300)->save($small_image_path);
            $product->images = $filename;

            $image = new Image();
            $image->name = $filename;
            $image->save();
        }
    }
} 
WebbieWorks's avatar

I appreciate your response. Would the code need to be like this?

if($request->hasFile('image')){
            $image_tmp = Input::file('image');
if($request->hasFile('images[]')){
                $image_tmp = Input::file('images[]');
                if($image_tmp->isValid()){
                    $extension = $image_tmp->getClientOriginalExtension();
                    $filename = rand(111,9999).'.'.$extension;
                    $large_image_path = 'images/backend_img/products/large/'.$filename;
                    $medium_image_path = 'images/backend_img/products/medium/'.$filename;
                    $small_image_path = 'images/backend_img/products/small/'.$filename;
                    //Resize Images
                    Image::make($image_tmp)->save($large_image_path);
                    Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
                    Image::make($image_tmp)->resize(300,300)->save($small_image_path);
                    $product->image = $filename;
                }
            }
Sergiu17's avatar

No reason to do this, until you don't have an input with image name

WebbieWorks's avatar

I understand, but it still is not storing the 2 other images into the database, just the main one. I need to upload a main image, then upload multiple images for the gallery. The main image uploads and gets stored, but the gallery images do not.

<div class="control-group">
     <label class="control-label">Main Image :</label>
        <div class="controls">
                <input type="file" name="image" id="image"/>
        </div>
</div>
<div class="control-group">
    <label class="control-label">Gallery Images :</label>
         <div class="controls">
                <input type="file" name="images[]" id="images" multiple />
         </div>
</div>
Sergiu17's avatar
if($request->hasFile('image')){
            $image_tmp = Input::file('image');
if($request->hasFile('images[]')){ // <----- REMOVE SQUARE BRACKETS!

Remove square brackets

if($request->hasFile('images')){

this is how you need to check images from gallery

Please or to participate in this conversation.