我试图将文件上传到服务器.但它不起作用的地方.这是我的表格.
<form action="index/upload" method="POST">
<label>File</label>
<input type="file" name="upFile">
<input type="submit" name="upload">
</form>
Run Code Online (Sandbox Code Playgroud)
这是我的控制器
public function uploadAction()
{
$this->view->disable();
if ($this->request->hasFiles() == true) {
foreach ($this->request->getUploadedFiles() as $file){
echo $file->getName(), ' ', $file->getSize(), '\n';
}
} else {
echo 'File not uploaded';
}
}
Run Code Online (Sandbox Code Playgroud)
但它总是返回"文件未上传".
你的PHP代码是正确的,问题出在你的HTML.您应该在表单中添加正确的编码:
<form action="index/upload" method="POST" enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)
更多信息:enctype ='multipart/form-data'是什么意思?