使用<form>元素上传文件,但$request->file()始终返回null
前端的相关代码
<form action="/api/upload" method="post">
<input type="file" name="ff" id="ff">
<button type="submit">GO</button>
</form>
后端的相关代码
public function upload(Request $request)
{
$file = $request->file('ff');
if ($file->isValid()) {
return response($file->getPath());
} else {
return response('no');
}
}
Error: Call to a member function isValid() on null in D:\Projects\school-e-board\app\controller\ApiController.php:85
Stack trace:
#0 D:\Projects\school-e-board\vendor\workerman\webman-framework\src\App.php(318): app\controller\ApiController->upload()
#1 D:\Projects\school-e-board\vendor\workerman\webman-framework\src\App.php(348): Webman\App::Webman\{closure}()
#2 D:\Projects\school-e-board\app\middleware\Login.php(25): Webman\App::Webman\{closure}()
#3 D:\Projects\school-e-board\vendor\workerman\webman-framework\src\App.php(340): app\middleware\Login->process()
#4 D:\Projects\school-e-board\vendor\workerman\webman-framework\src\App.php(167): Webman\App::Webman\{closure}()
#5 D:\Projects\school-e-board\vendor\workerman\workerman\Connection\TcpConnection.php(646): Webman\App->onMessage()
#6 D:\Projects\school-e-board\vendor\workerman\workerman\Events\Select.php(311): Workerman\Connection\TcpConnection->baseRead()
#7 D:\Projects\school-e-board\vendor\workerman\workerman\Worker.php(1479): Workerman\Events\Select->loop()
#8 D:\Projects\school-e-board\vendor\workerman\workerman\Worker.php(1399): Workerman\Worker::forkWorkersForWindows()
#9 D:\Projects\school-e-board\vendor\workerman\workerman\Worker.php(560): Workerman\Worker::forkWorkers()
#10 D:\Projects\school-e-board\vendor\workerman\webman-framework\src\support\App.php(131): Workerman\Worker::runAll()
#11 D:\Projects\school-e-board\start.php(4): support\App::run()
#12 {main}
操作系统: Window 11
php 版本: 8.2.2
webman 版本: 4.1.8
webman 版本:1.5.1
解决了,用异步请求上传的方式就行
前端html写错了,html表单上传要加
enctype="multipart/form-data"
原来问题根源在这!太感谢了