在 cakephp 2.3 中上传文件 [英] file upload in cakephp 2.3
问题描述
我是 cakephp 的新手,我正在尝试使用 cakephp 2.3 创建一个简单的文件上传这里是我的控制器
公共函数 add() {如果 ($this->request->is('post')) {$this->Post->create();$文件名 = 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('您的帖子已保存.');$this->redirect(array('action' => 'index'));} 别的 {$this->Session->setFlash('无法添加你的帖子.');}}}
和我的 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('提交')
它在数据库中保存了名字、姓氏、关键字和文件名,但是我想保存在 app/webroot/documents 中的文件没有保存,有人可以帮忙吗?谢谢
<块引用>更新
thaJeztah 我按照你说的做了,但如果我没记错的话,它会出现一些错误,这是控制器
公共函数 add() {如果 ($this->request->is('post')) {$this->Post->create();$文件名 = 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('您的帖子已保存.');$this->redirect(array('action' => 'index'));} 别的 {$this->Session->setFlash('无法添加你的帖子.');}}}
<块引用>
和我的 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('提交')
<块引用>
错误是
注意(8):数组到字符串的转换[CORECakeModelDatasourceDboSource.php,第 1005 行]
数据库错误错误:SQLSTATE[42S22]:未找到列:1054 未知字段列表"中的数组"列
SQL 查询:INSERT INTO first.posts(名字、姓氏、关键字、doc_file) VALUES ('dfg', 'cbhcfb', 'dfdbd', Array)
和维克多我也做了你的版本,它也不起作用.
您似乎使用了错误的密钥"来访问发布的数据;
$this->data['posts'][....
应该匹配你模型的别名";单数和一个大写首字母
$this->data['Post'][....
另外,$this->data
是 $this->request->data
的包装器,用于向后兼容,所以最好使用它;
$this->request->data['Post'][...
要检查发布数据的内容并了解其结构,您可以使用它进行调试;
debug($this->request);
只需确保启用调试,通过在 app/Config/core.php<中将
debug
设置为 1
或 2
/代码>
更新;重复的表单标签!
我刚刚注意到您还在代码中创建了多个(嵌套)表单;
echo $this->Form->input('keywords');//这会在前一个表单中创建另一个表单!echo $this->Form->create('Post', array('type' => 'file'));echo $this->Form->input('doc_file',array('type' => 'file'));
嵌套表单将永远工作,删除该行并添加'type =>;文件'到第一个 Form->create()
仅使用文件名称作为数据库
数组到字符串的转换" 问题是由于您试图直接将doc_file"的数据用于数据库这一事实造成的.因为这是一个文件上传字段,'doc_file' 将包含一个 Array 数据('name'、'tmp_name' 等).
对于您的数据库,您只需要该数组的名称",因此您需要在将数据保存到数据库之前对其进行修改.
例如例如这种方式;
//初始化文件名变量$文件名=空;如果 (!empty($this->request->data['Post']['doc_file']['tmp_name'])&&is_uploaded_file($this->request->data['Post']['doc_file']['tmp_name'])){//剥离路径信息$filename = basename($this->request->data['Post']['doc_file']['name']);move_uploaded_file($this->data['Post']['doc_file']['tmp_name'],WWW_ROOT .DS.'文件' .DS.$文件名);}//设置文件名只保存在数据库中$this->data['Post']['doc_file'] = $filename;
I'm new in cakephp and i'm trying to create a simple file upload with cakephp 2.3 here is my controller
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.');
}
}
}
and my 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')
it saves firstname, lastname, keywords, and the name of the file in DB , but the file which i want to save in app/webroot/documents is not saving , can anyone help ? Thanks
Update
thaJeztah i did as u said but it gives some errors here is controller if i'm not wrong
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.');
}
}
}
and my 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')
and the errors are
Notice (8): Array to string conversion [CORECakeModelDatasourceDboSource.php, line 1005]
Database Error Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
SQL Query: INSERT INTO first.posts (firstname, lastname, keywords, doc_file) VALUES ('dfg', 'cbhcfb', 'dfdbd', Array)
and Victor i did your version too , it doesnt work too .
You seem to be using the wrong 'key' to access the posted data;
$this->data['posts'][....
Should match the 'alias' of you Model; singular and a captial first letter
$this->data['Post'][....
Also, $this->data
is a wrapper for $this->request->data
for backwards compatibility, so it's better to use this;
$this->request->data['Post'][...
To check the content of the posted data and understand how it's structured, you may debug it using this;
debug($this->request);
Just be sure to enable debugging, by setting debug
to 1
or 2
inside app/Config/core.php
Update; duplicate Form tags!
I just noticed you're also creating multiple (nested) forms in your code;
echo $this->Form->input('keywords');
// This creates ANOTHER form INSIDE the previous one!
echo $this->Form->create('Post', array( 'type' => 'file'));
echo $this->Form->input('doc_file',array( 'type' => 'file'));
Nesting forms will never work, remove that line and add the 'type => file' to the first Form->create()
Using only the file name for the database
The "Array to string conversion" problem is cause by the fact that you're trying to directly use the data of 'doc_file' for your database. Because this is a file-upload field, 'doc_file' will contain an Array of data ('name', 'tmp_name' etc.).
For your database, you only need the 'name' of that array so you need to modify the data before saving it to your database.
For example this way;
// Initialize filename-variable
$filename = null;
if (
!empty($this->request->data['Post']['doc_file']['tmp_name'])
&& is_uploaded_file($this->request->data['Post']['doc_file']['tmp_name'])
) {
// Strip path information
$filename = basename($this->request->data['Post']['doc_file']['name']);
move_uploaded_file(
$this->data['Post']['doc_file']['tmp_name'],
WWW_ROOT . DS . 'documents' . DS . $filename
);
}
// Set the file-name only to save in the database
$this->data['Post']['doc_file'] = $filename;
这篇关于在 cakephp 2.3 中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!