mardi 30 août 2016

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);
        Images::create([
          '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,

Robin



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire