在cakephp中上传文件

时间:2016-10-10 06:39:34

标签: cakephp

我是Cakephp的新手。我正在使用cakephp2.8.5版本。我正在尝试从HTML代码上传文件,但它没有上传任何文件和表单正在提交。

View page add.ctp:
  <form name="add_userform" class="form-horizontal" role="form" accept-charset="utf-8" enctype="multipart/form-data" method="post" id="UserAddForm" action="/invl_exams/users/add">
    <div class="form-group" id="ShowDoc">
        <label for="usersFile">File</label>
        <input type="file" name="data[User][doc_file]" id="usersFile"/>
    </div>
 </form>   
Controller Page is UsersController.php:

    public function add()
    {
        if($this->request->is('post'))
        {
        $this->User->create();
        $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
        $filename = "app/webroot/files/".$this->data['User']['doc_file']['name'];       

            if (move_uploaded_file($this->data['User']['doc_file']['tmp_name'],$filename))
            {               
                if($this->User->save($this->request->data))   
                {    
                    $this->redirect('addExam');
                } 
            }
        }        
}

1 个答案:

答案 0 :(得分:0)

您可以使用此插件在Cakephp 2.x中上传图片。它将处理验证错误,如文件类型,文件大小等。

插件链接 - https://github.com/josegonzalez/cakephp-upload/tree/2.x