我是 cakephp 新手,我正在尝试使用 cakephp 2.3 创建一个简单的文件上传,这是我的控制器
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
$filename = WWW_ROOT. DS . 'documents'.DS.$this->data['posts']['doc_file']['name'];
move_uploaded_file($this->data['posts']['doc_file']['tmp_name'],$filename);
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
和我的 add.ctp
echo $this->Form->create('Post');
echo $this->Form->input('firstname');
echo $this->Form->input('lastname');
echo $this->Form->input('keywords');
echo $this->Form->create('Post', array( 'type' => 'file'));
echo $this->Form->input('doc_file',array( 'type' => 'file'));
echo $this->Form->end('Submit')
它将名字、姓氏、关键字和文件名保存在数据库中,但是我想保存在 app/webroot/documents 中的文件没有保存,有人可以帮忙吗?谢谢
更新
thaJeztah 我按照你说的做了,但是如果我没记错的话,这里会出现一些错误是控制器
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
$filename = WWW_ROOT. DS . 'documents'.DS.$this->request->data['Post']['doc_file']['name'];
move_uploaded_file($this->data['posts']['doc_file']['tmp_name'],$filename);
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
和我的 add.ctp
echo $this->Form->create('Post', array( 'type' => 'file'));
echo $this->Form->input('firstname'); echo $this->Form->input('lastname');
echo $this->Form->input('keywords');
echo $this->Form->input('doc_file',array( 'type' => 'file'));
echo $this->Form->end('Submit')
错误是
注意 (8):数组到字符串的转换 [CORE\Cake\Model\Datasource\DboSource.php,第 1005 行]
数据库错误错误:SQLSTATE [42S22]:未找到列:1054“字段列表”中的未知列“数组”
SQL 查询: INSERT INTO first.posts (firstname, lastname, keywords, doc_file) VALUES ('dfg', 'cbhcfb', 'dfdbd', Array)
和维克多我也做了你的版本,它也不起作用。