【发布时间】:2018-11-29 11:06:48
【问题描述】:
我正在尝试使用 cakephp 2 创建多文件上传。所以请帮忙。我是 cakephp 新手 如您所见,我的控制器和视图编码正在为单个图像工作。
public function admin_add() {
if ($this->request->is('post')) {
$this->Portfolio->create();
$error_message = $this->Portfolio->checkFileSize($this->request->data);
if( $error_message === true ) {
$this->request->data['Portfolio'] = $this->Common->processMedia($this->request->data['Portfolio']);
//echo '<pre>'; print_r($this->request->data); echo '</pre>';
// exit();
if ($this->Portfolio->save($this->request->data)) {
$this->Session->setFlash(__('The portfolio has been saved'), 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The portfolio could not be saved. Please, try again.'), 'flash_error');
}
} else {
$this->Session->setFlash(__($error_message), 'flash_error');
}
}
$talents = $this->Portfolio->Talent->find('list');
$skills = $this->Portfolio->Skill->find('list', array(
'order' => array('Skill.name ASC')
));
$this->set(compact('talents', 'skills'));
}
查看 -
<input type="file" name="data[Portfolio][media_url_file][]" class=" validate[] m-wrap large" id="PortfolioMediaUrlFile" multiple>
这是我的数据库结构 数据库:-enter image description here
生成的数组- . 使用 foreach 无法将此数组保存在数据库中。
Array
(
[Portfolio] => Array
(
[talent_id] => 435
[title] => asas
[status] => Approved
[type_of_media] => picture
[file] => Array
(
[0] => Array
(
[name] => wi-logo.png
[type] => image/png
[tmp_name] => /tmp/phpn8ne7a
[error] => 0
[size] => 17457
)
[1] => Array
(
[name] => unnamed.png
[type] => image/png
[tmp_name] => /tmp/phpPcqpQw
[error] => 0
[size] => 14362
)
)
[media_url] => Array
(
[0] => Array
(
[name] => wi-logo.png
[type] => image/png
[tmp_name] => /tmp/phpn8ne7a
[error] => 0
[size] => 17457
)
[1] => Array
(
[name] => unnamed.png
[type] => image/png
[tmp_name] => /tmp/phpPcqpQw
[error] => 0
[size] => 14362
)
)
)
)
【问题讨论】:
-
上传多张图片或保存数据时是否出错?是否要将多个图像名称保存在单个列中?
-
此代码用于单次上传。我想要多个上传。有多个列。
-
我正在尝试使用一个文件输入元素来上传多个文件。虽然文件选择器允许选择多个文件并创建多个数组。但是我如何使用 foreach 循环保存多个数组
-
请分享您的最终数组。
标签: php file cakephp-2.0