我正在使用CakePHP 2.1并尝试将文件上传到服务器。
我可以通过手动命名文件成功地将文件移动到指定的目录,但它不会从查看中的文件类型:注册。
数据库表:用户
Id Auto_Increment
username
file_name
控制器:用户控制器.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
}
}
}
查看 : 用户/注册.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';
但是它不能返回相同的错误,任何想法...
试试这个:
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);
}
}
}