文件上传时,无法在laravel 5中获得$ _FILES

时间:2015-04-09 14:45:29

标签: file-upload laravel-5

我目前正在工作laravel 5项目。我正在尝试上传文件,但我发现我无法在控制器功能中获得$ _FILES。它只显示[]当我写dd($ _ FILES);在我的控制器功能。  查看代码:

 {!!Form::open(['url'=>'admin','id'=>'MyUploadForm'])!!}
   <input name="fileupload" id="fileupload" type="file" />
   <input type="submit"  id="submit-btn">
 {!!Form::close()!!}

ROUTE CODE:

Route::get('admin', 'CouponcodefileController@index');
Route::post('admin', 'CouponcodefileController@uploadfile');

控制器功能:

public function uploadfile() {
      dd($_FILES);
    }

请在此指导。感谢。

2 个答案:

答案 0 :(得分:1)

尝试:

  

&#34;文件&#34; =&GT;真

查看

{!!Form::open(['url'=>'admin','id'=>'MyUploadForm', 'method'=>'POST', 'files'=>true])!!}
    <input name="fileupload" id="fileupload" type="file" />
    <input type="submit"  id="submit-btn">
{!!Form::close()!!}

控制器

public function uploadfile() {
    $file       = \Input::file('fileupload'));
    // $file    = \Request::file('fileupload'));

    $filename  = $file->getClientOriginalName();
    $extension = $file->getClientOriginalExtension();

    dd($file);
}

http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/File/UploadedFile.html

答案 1 :(得分:1)

以Laravel的方式(5.1):

控制器功能:

public function uploadfile(Request $request) {
    // Laravel 5.0: $file = Request::file('fileupload');
    $file = $request->file('fileupload');    
    dd($file);
}

https://laravel.com/docs/5.1/requests#files