文件上传在cakephp 2.3 [英] file upload in cakephp 2.3
问题描述
我是cakephp新的,我试图用cakephp 2.3创建一个简单的文件上传这里是我的控制器
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.');
}
}
}
和我的add.ctp
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')
保存firstname,lastname,关键字和文件在DB中的名称,但我想保存在app / webroot /文件中的文件不保存,任何人都可以帮助?感谢
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
更新
thaJeztah我u说,但它给一些错误这里是控制器如果我没有错误
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.');
}
}
}
和我的add.ctp
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
注意(8):数组到字符串转换
[CORE\Cake\Model\Datasource\\ \\ DboSource.php,line 1005]
Notice (8): Array to string conversion [CORE\Cake\Model\Datasource\DboSource.php, line 1005]
数据库错误错误:SQLSTATE [42S22]:未找到列:1054未知
列'数组' '
Database Error Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
SQL查询:INSERT INTO first.posts(firstname,lastname,keywords,
doc_file)VALUES('dfg','cbhcfb','dfdbd' ,Array)
SQL Query: INSERT INTO first.posts (firstname, lastname, keywords, doc_file) VALUES ('dfg', 'cbhcfb', 'dfdbd', Array)
和Victor我也是你的版本,它也不工作。
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'][....
此外, $ this- > data
是 $ this-> request-> data
的向后兼容性的包装,因此最好使用此方法; p>
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);
只要确保启用调试,通过设置 debug
到 1
或 2
里面 app / Config / core.php
Just be sure to enable debugging, by setting debug
to 1
or 2
inside app/Config/core.php
我注意到您也在代码中创建多个(嵌套)表单;
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'));
嵌套表单将永远不会工作,删除该行并添加'类型=> file'到第一个 Form-> create()
Nesting forms will never work, remove that line and add the 'type => file' to the first Form->create()
数组到字符串转换问题是由于你试图直接使用您的数据库的doc_file的数据。因为这是一个文件上传字段,'doc_file'将包含一个数据('name','tmp_name'等)。
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.
以这种方式 ;
// 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屋!