【问题标题】:upload file in cakephp without pluggin or using controller code无需插件或使用控制器代码在 cakephp 中上传文件
【发布时间】:2012-06-13 09:08:17
【问题描述】:

我正在学习 CakePHP 2.0,并创建了一个示例测试应用程序,用户可以在注册时提交文件。

下面是数据库表

用户表

id Auto_Increment
first_name
last_name
email
doc_file

并且还创建了User.phpUsersController.php

以下是UsersController.php中的内容代码

UsersController.php

class UsersController extends AppController{

    public $helpers = array('Html', 'Form');


    public function index(){
        $this->set('user1', $this->User->find('all'));
    }


    public function register(){
        if ($this->request->is('post')){
            if ($this->User->save($this->request->data)){
                //move_uploaded_file($this->data['Model']['field']['tmp_name'], WWW_ROOT.DS.'xxx');

                move_uploaded_file($this->data['User']['doc_file']['tmp_name'], WWW_ROOT.DS.'hello.doc');

                $this->Session->setFlash('User is created');
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash('Cannot register a user');
            }
        }
    }
}

在 Views 中,我创建了两个文件,即 index.ctpregister.ctp,在 View 目录中有一个目录 Users

register.ctp的代码内容

register.ctp

<?php

    echo $this->Form->create('User', array('type'=>'file'));
    echo $this->Form->input('first_name', array('label'=>'First Name'));
    echo $this->Form->input('last_name', array('label'=>'Last Name'));
    echo $this->Form->input('email');
    echo $this->Form->input('doc_file', array('type'=>'file'));
    echo $this->Form->end('Register');
?>  

当运行这个页面,并填写所有信息时,它给出了一个错误

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

下面是我得到的查询,它将array插入doc_file

SQL Query: INSERT INTO `database_db`.`users` (`first_name`, `last_name`, `email`, `doc_file`) VALUES ('master', 'shifu', 'shifu@k.com', Array)

我正在尝试什么:

用户注册时,文件名应该是随机的,应该移动到

localhost/mysite/app/webroot/files/user_data/localhost/mysite/app/webroot/files/user_data/user_id_directory/

很好,如果它创建用户 ID 目录并将文件存储在其父用户目录中

【问题讨论】:

    标签: cakephp file-upload cakephp-2.1


    【解决方案1】:

    问题是 CakePHP 看到 doc_file 字段并试图将其插入数据库。

    Cake 处理完表单提交后,doc_file 包含一个值数组。

    您可以通过执行以下操作来修复它:

    在你看来:

    echo $this->Form->input('new_doc_file', array('type'=>'file'));
    

    在您的控制器中:

    if ($this->request->is('post')){
        $this->data['User']['doc_file'] = WWW_ROOT.DS.'hello.doc';
        if ($this->User->save($this->request->data)){
            move_uploaded_file($this->data['User']['new_doc_file']['tmp_name'], $this->data['User']['doc_file']);
    
            $this->Session->setFlash('User is created');
            $this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash('Cannot register a user');
        }
    }
    

    (即使用临时字段名称上传文件,在尝试保存之前手动将 doc_file 字段添加到 post 数组)。

    【讨论】:

    • 看到这条语句$this-&gt;User-&gt;save($this-&gt;request-&gt;data) 正在将值存储到数据库中...我上传了文件,但是如何将名称插入到数据库表中。保存时,它会给出一个错误,即Database ErrorArray 不能插入到table 字段中。我们需要来自Array 的单个值。
    • @RicharAtHome 能否告诉我,如何在尝试保存之前手动将 doc_file 字段添加到 post 数组中
    • 我正在使用以下行手动将 doc_file 字段添加到 post 数组: $this->data['User']['doc_file'] = WWW_ROOT.DS.'hello.doc';
    • 然后使用一些简单的正则表达式或字符串切片从 new_doc_file 字段中提取名称:-)
    【解决方案2】:

    错误提示

    $this->data['User']['doc_file']['tmp_name']
    

    正在返回一个数组 - 尝试

    debug($this->data['User']['doc_file']['tmp_name']) 
    

    在您的控制器代码中查看它包含的内容。

    【讨论】:

    • 它返回类似D:\xampp\tmp\phpF639.tmp\app\Controller\UsersController.php (line 19)
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多