【发布时间】:2017-03-02 23:51:55
【问题描述】:
我正在尝试上传文件,但它总是在我的控制器中返回 null。这是我得到的错误:
FatalThrowableError in FileController.php line 25:
Fatal error: Call to a member function getClientOriginalExtension() on null
表格
<form enctype="multipart/form-data" method="post" action="{{route('upload')}}">
<label for="file_upload">Upload file</label>
<input id="file_upload" type="file" name="file">
<input type="submit" value="Upload">
</form>
路由(不调用任何中间件)
Route::post('file/upload', [
'as' => 'upload',
'uses' => 'FileController@handleUpload',
]);
控制器
public function handleUpload(Request $request)
{
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'adp';
if (!file_exists($dir)) mkdir($dir);
$extensions = AppPlatform::lists('package_extension')->toArray();
$extension = $request->file('file')->getClientOriginalExtension(); // this line throws the nullreference exception
...
}
我可能忽略了一些非常明显的问题,但目前似乎找不到问题所在。
编辑
当我在 chrome 的开发人员工具中查看请求时,我可以看到文件在 post 请求中正确发送。
------WebKitFormBoundaryTLB02G6sLMEE2fMM
Content-Disposition: form-data; name="file"; filename="app-release.apk"
Content-Type: application/octet-stream
------WebKitFormBoundaryTLB02G6sLMEE2fMM--
【问题讨论】:
-
尝试将
$_FILES转储到handleUpload()。什么是输出? -
你在 dd($request->file('file')) 中得到什么了吗?
-
验证服务器是否允许文件上传。检查您的
PHP.ini并找到file_uploads = On(验证它是否开启) -
你试过上传图片吗?
-
@Fester 酷!。编码愉快。
标签: php laravel laravel-5.2