注意:未定义的索引:上传文件时的文件

时间:2019-02-23 10:42:35

标签: php html

我正在尝试在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方法检查表单方法是否为邮政

1 个答案:

答案 0 :(得分:0)

对于下一个正在寻找的人:)

要上传文件,您需要enctype="multipart/form-data"

<form enctype="multipart/form-data">
    <input type="file" name="file"/>
</form>