Robin R Robin R - 1 year ago 91
PHP Question

Laravel uploading multiple images doesn't work, also mimes don't

I'm trying to upload images in Laravel using this code:

public function postAddPictures(Request $request)

// getting all of the post data
$files = $request->file('cover_image');
// Making counting of uploaded images
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {

$messages = [
'cover_image.required' => 'U moet een afbeelding opgeven.',
'cover_image.image' => 'De bestanden moeten een afbeelding zijn (jpeg, png, bmp, gif, or svg).',
'description.required' => 'U moet een beschrijving opgeven.'

$rules = [
'cover_image' => 'required',//|mimes:png,gif,jpeg,jpg,bpm,svg
'album_id' => 'required|numeric|exists:albums,id',
'description' => 'required'

$validate = ['file'=> $file, 'description' => $request->get('description'), 'album_id'=> $request->get('album_id')];

$validator = Validator::make($validate, $rules, $messages);

if ($validator->fails()) {
return Redirect::to('admin/pictures/add')->withInput()->withErrors($validator);

$random_name = str_random(8);
$destinationPath = 'public/uploads/pictures/rallypodium/website/'.Album::find($request->get('album_id'))->type.'/'.Album::find($request->get('album_id'))->name.'/';
$extension = $file->getClientOriginalExtension();
$filename = $random_name.'_album_image.'.$extension;
$uploadSuccess = $file->move($destinationPath, $filename);
'description' => $request->get('description'),
'image' => $filename,
'album_id'=> $request->get('album_id')

$uploadcount ++;


if($uploadcount == $file_count){
Activity::log('heeft foto's in de map map "'.ucwords(str_replace('-', ' ', Album::find($request->get('album_id'))->name)).'" toegevoegd.');
$request->session()->flash('alert-success', 'Foto's succesvol toegevoegd.');
return Redirect::to('admin/pictures/add');

The problem here is, it keeps returning the error message 'U moet een afbeelding opgeven.'. It doesn't store the data in the database nor uploads the files.

This are my fields in HTML:

  • cover_image

  • album_id

  • description

Could someone help me out? I tried different ways already but I can't find the solution at all.

Kindest regards,


Answer Source

Instead of a key 'file' you should use key 'cover_image' if you want to validate all files one by one.

$validate = ['cover_image'=> $file, 'description' => $request->get('description'), 'album_id'=> $request->get('album_id')];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download