首页 文章

上传时在CakePHP中的db not array中插入文件名

提问于
浏览
1

我正在使用CakePHP 2.1并尝试将文件上传到服务器 .

我可以通过手动命名文件成功地将文件移动到指定目录,但是它不从视图中的文件类型中取名:register .

数据库表:用户

Id Auto_Increment
username
file_name

Controller:UsersController.php

public function register(){
    if ($this->request->is('post')){
        //   $this->data['User']['resume_file']['tmp_name'] = 'test.php';
        //  The above statement is working good

        if ($this->User->save($this->request->data)){
            $this->Session->setFlash('Message : ' . $this->data['User']['resume_file']['tmp_name']);
            //debug($this->data['User']['resume_file']['tmp_name']);

            // This is giving an error, while inserting
        }
    }
}

查看:users / register.php

<?php
    echo $this->Form->create('User', array('type'=>'file'));
    echo $this->Form->input('username');
    echo $this->Form->input('file_name', array('type'=>'file', 'label'=>'Upload document') );
    echo $this->Form->end('Register');
?>

错误:数据库错误

INSERT INTO `sample_db`.`users` (`file_name`, `created`) VALUES (Array, '2012-06-14 08:10:10')

我看到的是,当插入到表中时, Array 会在CakePHP中插入那个和错误 Array to string conversion 错误,现在怎样才能在CakePHP中获取 input type : file_name 的值

或者,如果我们可以在使用以下请求语句保存之前修改数据 .

$this->request->data

我尝试用下面的声明来改变它

$this->data['User']['resume_file']['tmp_name'] = 'test.php';

但无法回复相同的错误,任何想法......

1 回答

  • 3

    试试这个:

    public function register(){
        if ($this->request->is('post')){
            $filename = $this->request->data['User']['file_name']['tmp_name'];
            $this->data['User']['file_name'] = $filename;
    
            if ($this->User->save($this->request->data)){
                $this->Session->setFlash('Message : ' . $filename);
            }
        }
    }
    

相关问题