不能使用Symfony \ Component \ HttpFoundation \ File \ UploadedFile类型的对象作为数组Laravel 4.2文件上传

时间:2015-08-26 11:53:27

标签: php arrays symfony laravel-4

我正在尝试使用Laravel 4.2同时上传大量文件,但它不起作用。

当我尝试上传2个或更多文件时,laravel仅上传最新选择的文件。

现在我改变了一些东西,它给了我错误Cannot use object of type Symfony\Component\HttpFoundation\File\UploadedFile as array

为什么会返回此错误?为什么他不上传所有图片?

我的控制器:

public function postUpload() {
  // getting all of the post data
  $files = Input::file('file');
  //echo "<pre>";
  //var_dump($files);
  //echo "</pre>";
  //die;
  $map = Input::get('mapname');
  // setting up rules
  $rules = array('file' => 'max:10000'); //mimes:jpeg,bmp,png and for max size max:10000
  // doing the validation, passing post data, rules and the messages
  $validator = Validator::make($files, $rules);
  if ($validator->fails()) {
    // send back to the page with the input data and errors
    Session::flash('error_message', 'Er ging iets mis!');
    return Redirect::to('admin/img/upload')->withInput()->withErrors($validator);
  }
  else {
    // checking file is valid.

        if($files)
        {
            //echo "<pre>";
            //var_dump(Input::hasFile('file'));
            //echo "</pre>";
            //die;
            foreach($files as $file)
            {
                $destinationPath = 'public/pictures/overall/'.$map.'/'; // upload path
                $filename = str_random(40).'_'.$file[0]->getClientOriginalName();
                $extension = $file[0]->getClientOriginalExtension(); // getting image extension
                $file[0]->move($destinationPath, $filename); // uploading file to given path
            }
        // sending back with message
        Session::flash('success', 'Succesvol geüpload!'); 
        return Redirect::to('admin/img/upload');
        }

    else {
      // sending back with error message.
      Session::flash('error_message', 'Er ging iets mis!');
      return Redirect::to('admin/img/upload');
    }
  }
}

我使用的视图:

{{ Form::open(array('action' => 'AdminPictureController@PostUpload', 'accept-charset' => 'UTF-8', 'files' => true)) }}
<select id="mapname" name="mapname">
@foreach (array_reverse($folders) as $folder)
<option value="{{ $folder }}">{{ str_replace('-', ' ', $folder) }}</option>
@endforeach
</select>
<br><br>
{{ Form::file('file[]', ['multiple' => true]) }}
<br>
<button type="submit" class="btn btn-success">Uploaden</button>
{{ Form::close() }}

呈现的视图:

<form method="POST" action="http://localhost/RPR/admin/img/uploadfile" accept-charset="UTF-8" enctype="multipart/form-data">
<input name="_token" type="hidden" value="SkMoFqiOYBJZOOrvcwtMUGGjHV6gPftAq2mPE6Uz">
<select id="mapname" name="mapname">
<option value="TAC-Tielt-Shakedown-2015">TAC Tielt Shakedown 2015</option>
<option value="TAC-Tielt-2013">TAC Tielt 2013</option>
<option value="Rally-van-Staden-2015">Rally van Staden 2015</option>
</select>
<br><br>
<input multiple="1" name="file[]" type="file">
<br>
<button type="submit" class="btn btn-success">Uploaden</button>
</form>

当我按Uploaden(提交按钮)时出现错误。 当我按下按钮时,它会转到路线Route::post('img/uploadfile', 'AdminPictureController@PostUpload');

所以他拿走了控制器。但是哪里出错了,我不知道。他强调了这一点:http://prntscr.com/892dbt

希望有人知道答案。

最诚挚的问候,

罗宾

1 个答案:

答案 0 :(得分:6)

嗯,让我看看我是否做对了。

// Should be array of "UploadedFile" objects
$files = Input::file('file');

if($files)
{
    // Iterating over the array
    // "file" should be an instance of UploadedFile
    foreach($files as $file)
    {
        // ** You cannot use $file[0] **
        $filename = str_random(40).'_'.$file[0]->getClientOriginalName();

        // Instead, use this
        $filename = str_random(40).'_'.$file->getClientOriginalName();

    }
}

如上所述,$file[0]是导致Cannot use object of type UploadedFile as array错误的明确原因。

这仍然无法解决显示单个文件而不是多个文件的问题。你确定这仍然是个问题吗?