gpt4 book ai didi

php - 上传时在数据库中插入文件名而不是 CakePHP 中的数组

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:44 24 4
gpt4 key购买 nike

我正在使用 CakePHP 2.1 并尝试将文件上传到服务器。

我可以通过手动命名文件成功地将文件移动到指定目录,但它没有从 View 中的文件类型:注册。

数据库表:用户

Id Auto_Increment
username
file_name

Controller :UsersController.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
}
}
}

查看:users/register.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);
}
}
}

关于php - 上传时在数据库中插入文件名而不是 CakePHP 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11028891/

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