【发布时间】:2014-04-05 14:05:49
【问题描述】:
上传文件时,您通常会为该字段创建一个数组。比如模型是Document,字段是submittedfile:
$this->request->data['Document']['submittedfile'] = array(
'name' => 'conference_schedule.pdf',
'type' => 'application/pdf',
'tmp_name' => 'C:/WINDOWS/TEMP/php1EE.tmp',
'error' => 0,
'size' => 41737,
);
无法将数组保存到数据库,因为该字段无法将 'submittedfile' 数组保存到数据库 'submittedfile' 列。但是,我只需要数据库的 submittedfile 字段中上述数组的 name 部分。尝试了很多方法这里有两种:
1在控制器方法中调用保存方法之前,将提交的文件从数组转换为字符串在控制器方法中。 (问题无法使用 cakePHP 验证 fileSize 和 uploadError,因为它们需要 ['tmp_name'] 才能工作。)
public function addDocument(){
$this->request->data['Document']['submittedfile'] = $this->request->data['Document']['submittedfile']['name'];
$this->Document->save($this->request->data);}
2在Controller方法中调用save方法之前,在beforeSave回调方法中将提交的文件从数组转为字符串。 (问题是我得到错误 Illegal string offset 'name')
public function beforeSave($options = array()){
$this->data['Document']['submittedfile'] = $this->data['Document']['submittedfile']['name'];}
关于将上传的文件类型数组保存到数据库的任何答案?
【问题讨论】:
标签: arrays file cakephp upload