【发布时间】:2018-08-25 08:05:05
【问题描述】:
我正在使用 laravel 5.6 使用 HTML 表单上传文件,但在控制器中检索文件时遇到问题。
当我使用这种方式检索它时,它返回 ok return $request;它正在控制器中检索,但不在 hasFile() 函数中或
getClientOriginalExtension()
我已经搜索了很多网站,但问题仍然存在。所有我匹配但不工作。
错误:“在 null 上调用成员函数 getClientOriginalExtension()”
HTML 表单视图:
<form action="{{url('/changeProductImage')}}" enctype="multipart/form-data" method="post">
{{csrf_field()}}
<input type="file" name="photo" id="photo" class="btn btn-secondary">
<hr>
<button type="submit" value="upload" class="form-control btn btn-primary btn-fill btn-lg">Upload Image</button>
</form>
路线:
Route::post('/changeProductImage','ProductsController@upload');
控制器:
function upload(Request $request)
{
$image = $request->file('photo');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return back()->with('success', 'Image Uploaded Successfully');
}
【问题讨论】:
-
var_dump($request->file('photo'))看看你得到了什么。 -
兄弟它返回 (NULL) 它总是返回 null 无法理解为什么??
-
如果你在
$image = $request->file('photo');上面做dd($request->all())会得到什么? -
找不到文件方法
标签: php laravel-5 laravel-5.6 laravel-request