Laravel多个项目上的多个文件

时间:2016-01-11 12:48:16

标签: php laravel

我正在使用Laravel Collective构建一个包含多个产品的表单,每个产品都有多个图像。当我尝试获取图像时,只有第一张上传的图像来自请求:

<div class="form-group{{ $errors->has('product[0][images]') ? ' has-error' : '' }}">
  {!! Form::label('product[0][images]', 'Images *', array('class' => 'col-md-4 control-label')) !!}
  <div class="col-md-6">
    {!! Form::file('product[0][images]', array('multiple' => true, 'class' => 'form-control')) !!}                        
    @if ($errors->has('product[0][images]'))
      <span class="help-block">
        <strong>{{ $errors->first('product[0][images]') }}</strong>
      </span>
    @endif
  </div>                        
</div>

// Controller
$inputs = $request->all();
var_dump($inputs);
die;

结果:

  

array(10){[“_token”] =&gt; string(40)“qax8yTQ8BTqPTME3bxh64U8CzZr1M2c6hzPk4Ijr”[“product”] =&gt; array(1){[0] =&gt; array(1){[“images”] =&gt; object(Symfony \ Component \ HttpFoundation \ File \ UploadedFile)#30(7){[“test”:“Symfony \ Component \ HttpFoundation \ File \ UploadedFile”:private] =&gt; bool(false)[“originalName”:“Symfony \ Component \ HttpFoundation \ File \ UploadedFile”:private] =&gt; string(12)“image1.jpg” [“mimeType”:“Symfony \ Component \ HttpFoundation \ File \ UploadedFile”:private] =&gt; string(10)“image / jpeg”[“size”:“Symfony \ Component \ HttpFoundation \ File \ UploadedFile”:private] =&gt; int(75336)[“error”:“Symfony \ Component \ HttpFoundation \ File \ UploadedFile”:private] =&gt; int(0)[“pathName”:“SplFileInfo”:private] =&gt; string(24)“C:\ xampp \ tmp \ php3422.tmp”[“fileName”:“SplFileInfo”:private] =&gt; string(11)“php3422.tmp”}}}}

我尝试使用:

  • 产物[0] [图像]
  • 产物[0] .images []
  • 产物[0] [图像[]]

没有成功。我做错了什么?

2 个答案:

答案 0 :(得分:1)

查看:

{!! Form::open(array('url'=> array('/pictures-all-tour', $id), 'method'=>'POST', 'files'=>true)) !!}
        <div class="control-group">
          <div class="controls">
          {!! Form::file('images[]', array('multiple'=>true,'class'=>'send-btn')) !!}
         </div>
        </div>

            <button type="submit" class="btn btn-primary publishtourbutt2">
                Save Photos
            </button>

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

控制器:

public function picturesalltour($id)
    {


    $files = Input::file('images');
    $file_count = count($files);

    $destinationPath = public_path().'/img/';

    $uploadcount = 0;

    foreach($files as $file) {
      $rules = array('file' => 'required');

        $extension = $file->getClientOriginalExtension();
        $fileName = rand(11111,99999).'.'.$extension;
        $file->move($destinationPath, $fileName);
        Image::make($destinationPath.$fileName)->fit(1200, 800)->save($destinationPath.$fileName);     

        $uploadcount ++;
        $newpic = 'pic'.$uploadcount;

        DB::table('tours')->where('id', $id)->update([
            $newpic => $fileName
            ]);

        DB::table('photos')->insert([
            'tourid' => $id,
            'userid' => Auth::user()->id,
            'picname' => $fileName
            ]);

    }


    \Session::flash('flash_message', "Pictures successfully added.");

    if($uploadcount == $file_count){
      return redirect('/photos-tour-get/'.$id);
    } 
    else {
      return redirect('/');
    }

    }
  

不知道你的情况,但我这样使用它并且它完美地工作,希望它有所帮助。

答案 1 :(得分:0)

我明白了。我应该用

{!! Form::file('products[0][images][]', array('multiple' => true, 'class' => 'form-control')) !!}