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
- First it was the
save()
outside theforeach
loop. - 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 thetime()
so don't trust thetime()
when you are uploading multiple files here I replaced it withstr_random( 10 )
and every this was OK after that.
if anybody have any comments or suggesting will be happy to hear from him.