我正在尝试在MVC框架中进行文件检查,即使我将文件放置为jpg格式,我也会得到error
这是检查格式的功能
public function addAction()
{
$upload = new Upload();
if($this->request->isPost())
{
$allowed = array('gif','png' ,'jpg');
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed))
{
echo 'error';
}
$this->request->csrfCheck();
$upload->assign($this->request->get());
$upload->user_id = Users::currentUser()->id;
if($upload->save())
{
Router::redirect('upload');
}
}
$this->view->uploas = $upload ;
$this->view->displayErrors = $upload->getErrorMessages();
$this->view->postAction = PROOT . 'upload' . DS . 'add';
$this->view->render('upload/add');
}
这是HTML代码:
<div class="col-lg-6 upload-position center" >
<input type="file" id="file" name="file" >
</div>
isPost
方法检查表单方法是否为邮政
答案 0 :(得分:0)
对于下一个正在寻找的人:)
要上传文件,您需要enctype="multipart/form-data"
:
<form enctype="multipart/form-data">
<input type="file" name="file"/>
</form>