Laravel 5.2 : Upload multiple image with intervention but First image only uploaded

799 views Asked by At

Hi I am trying to upload multiple images using the intervention package but when I start upload one image only gets upload

here is the view

{!! Form::open(array('url'=>'admin/event-gallery', 'method'=>'post', 'files'=>'true')) !!}
                {!! Form::label('image', 'Event Image Gallery') !!}
                {!! Form::file('image[]', ['multiple']) !!}
                {!! Form::hidden('event_id', $event->id) !!}
                {!! Form::submit('Add More Images', array('class'=>'btn btn-primary')) !!}
{!! Form::close() !!}

and here is the controller

    public function store( Request $request ) {
                $event = new EventGallery();
                if ( $request->hasFile( 'image' ) ) {
                    $images = $request->file( 'image' );
                    foreach ( $images as $image ) {
                        $event->event_id = $request->event_id;
                        $filename        = time() . '.' . $image->getClientOriginalExtension();
                        Image::make( $image )->resize( 450, 150 )->save( 'images/events/' . $filename );
                        Image::make( $image )->fit( 243, 111 )->save( 'images/events/thumbs-' . $filename );
                        $event->image = $filename;
                    }
                }
$event->save();
    Session::flash( 'status', 'Event has been added successfully' );

            return Redirect::to( 'admin/events/create' );
        }

Edit on how I got it work

  1. First it was the save() outside the foreach loop.
  2. Second I had a very strange issue here when I done with upload I found the images duplicated, and this was cased by this part of the code $filename = time() . '.' . $image->getClientOriginalExtension(); it was because the time() so don't trust the time() when you are uploading multiple files here I replaced it with str_random( 10 ) and every this was OK after that.

if anybody have any comments or suggesting will be happy to hear from him.

0

There are 0 answers