我正在使用cakephp 3.3。我可以上传单张图片,但不会在webroot / uploads下保存。我想上传多张图片并保存。我怎样才能实现它?请提供一些意见。我是PHP编程和这个框架的新手。谢谢!
`Images\add.ctp
<?= $this->Form->create($image,['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Image') ?></legend>
<?php
echo $this->Form->input('path',['type' => 'file']);
?>
</fieldset>`
ImagesController\add
public function add()
{
$image = $this->Images->newEntity();
if ($this->request->is('post')) {
$image = $this->Images->patchEntity($image, $this->request->data);
if ($this->Images->save($image)) {
$this->Flash->success(__('The image has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The image could not be saved. Please, try again.'));
}
}
$properties = $this->Images->Properties->find('list', ['limit' => 200]);
$this->set(compact('image', 'properties'));
$this->set('_serialize', ['image']);
}
Table\ImageTable.php
$validator
->requirePresence('path', 'create')
->notEmpty('path')
->add('processImageUpload', 'custom', [
'rule' => 'processImageUpload'
]);
public function processImageUpload($check = array()) {
if(!is_uploaded_file($check['path']['tmp_name'])){
return FALSE;
}
if (!move_uploaded_file($check['path']['tmp_name'], WWW_ROOT . 'img' . DS . 'images' . DS . $check['path']['name'])){
return FALSE;
}
$this->data[$this->alias]['path'] = 'images' . DS . $check['path']['name'];
return TRUE;
}
答案 0 :(得分:0)
在add.ctp中输入字段如下:
echo $this->Form->input('path[]',['type' => 'file','multiple'=>'multiple']);
在控制器中使用save方法:
// $image = $this->Images->newEntity();
$images= $this->Articles->newEntities($this->request->data);
if ($this->request->is('post')) {
// $image = $this->Images->patchEntity($image, $this->request->data);
if ($this->Images->saveMany($images)) {
$this->Flash->success(__('The image has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The image could not be saved. Please, try again.'));
}
}