Laravel上传多张图片并不起作用,也不是笑话

时间:2016-08-30 17:55:53

标签: php image laravel-5 image-uploading

我尝试使用以下代码在Laravel上传图片:

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');
      }
    }

问题在于,它不断返回错误信息' U moet een afbeelding opgeven。'。它不会将数据存储在数据库中,也不会上传文件。

这是我在HTML中的字段:

  • cover_image
  • album_id
  • 描述

有人可以帮帮我吗?我已经尝试了不同的方法,但我根本无法找到解决方案。

最诚挚的问候,

罗宾

1 个答案:

答案 0 :(得分:0)

而不是密钥'文件'你应该使用key' cover_image'如果你想逐个验证所有文件。

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