gpt4 book ai didi

cakephp - cakephp 2.3 中的文件上传

转载 作者:行者123 更新时间:2023-12-01 19:55:59 24 4
gpt4 key购买 nike

我是 cakephp 的新手,我正在尝试使用 cakephp 2.3 创建一个简单的文件上传,这是我的 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

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 中的文件没有保存,任何人都可以帮忙吗?谢谢

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 [CORE\Cake\Model\Datasource\DboSource.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 .

最佳答案

您似乎使用了错误的“ key ”来访问发布的数据;

$this->data['posts'][....

应与您模型的“别名”匹配; 单数大写首字母

$this->data['Post'][....

此外,$this->data$this->request->data 的包装器,用于向后兼容,因此最好使用它;

$this->request->data['Post'][...

要检查已发布数据的内容并了解其结构,您可以使用它进行调试;

debug($this->request);

只需确保启用调试,方法是将 app/Config/core.php< 中的 debug 设置为 12/

更新;重复的表单标签!

我刚刚注意到您还在代码中创建了多个(嵌套)表单;

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'));

嵌套表单永远无法工作,请删除该行并将“type => file”添加到第一个Form->create()

仅使用数据库的文件名称

“数组到字符串转换”问题是由于您尝试直接使用数据库的“doc_file”数据而引起的。因为这是一个文件上传字段,“doc_file”将包含一个数据数组(“name”、“tmp_name”等)。

对于您的数据库,您只需要该数组的“名称”,因此您需要在将数据保存到数据库之前修改数据。

例如这样;

// 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 - cakephp 2.3 中的文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16262194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com