Laravel:上传多张图片但仅上传第一张图片

时间:2016-09-22 12:38:41

标签: php laravel file-upload

我正在使用Laravel 4.2,我有这个表单上传多个图像

当我提交表单时它返回到视图页面而第一张图片只上传了。

任何人都可以查看我的代码并更正我的错误

{{ Form::open(array('url'=>'doAddProjectImage', 'files'=>'true', 'method'=>'PUT', 'class'=>'margin-top-30 width-100per pull-left')) }}
  {{ Form::file('img[]', array('class'=>'file', 'multiple'=>true)) }}
  {{ Form::submit('Add images to project', array('class'=>'btn-success btn pull-left')) }}
  {{ Form::hidden('pid', Session::get('insId')) }}
  {{ Form::close() }}

这是我的控制器

public function doAddProjectImage()
    {
        $proId = Input::get('pid');

        $projectImages = new ProjectsImages();
        $files = Input::file('img');
        foreach($files as $file) {
            $destination_path = 'images/projects/';
            $filename = str_random(6) . '_' . $file->getClientOriginalName();
            $file->move($destination_path, $filename);

            $projectImages->image = $filename;
            $projectImages->image_id = $proId;
            $projectImages->save();
        }

        return Redirect::to('admin/view-project');
    }

2 个答案:

答案 0 :(得分:3)

经过一些研究后,我发现'multiple'=>true错误应该是multiple

所以输入字段将是

{{ Form::file('img[]', array('class'=>'file', 'multiple')) }}

答案 1 :(得分:0)

尝试使用我的代码

<强>控制器

 public function img_upload($filename)
    {
        $photo = array('photo' => $filename);
        $destinationPath = 'uploads/multiple'; // upload path
        $original_filename = time().$filename->getClientOriginalName(); // getting image extension
        $extension = $filename->getClientOriginalExtension(); // getting image extension
        //$fileName = rand(11111,99999).'.'.$extension; // renameing image
        $filename->move($destinationPath, $original_filename); 
    }

    public function savemultiimage(Request $request)
    {   
        $files = $request->file('m_name');
        $file_count = count($request->file('m_name'));      
        foreach ($files as $file) {          
                $this->img_upload($file);
                $multi['m_name']=time().$file->getClientOriginalName(); 
                DB::table('multiimage')->insert($multi);
        }
        return redirect('/multifiles/gallery')->with('succ', 'Multiple image successfully inserted!');
    }

查看

{!! Form::open(array('url' => 'multifiles/savemultiimage','id'=>'multi_image','files'=>true)) !!}

    <div class="box-body">
        <div class="form-group">
          <label for="exampleInputFile">Multi Images</label>
          <input type="file" id="m_name" name="m_name[]" multiple>          
        </div> 
    </div><!-- /.box-body -->

    <div class="box-footer">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>

  {!! Form::close() !!}