0

我正在使用 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;

}

4

1 回答 1

0

在 add.ctp 中使输入字​​段类似于:

echo $this->Form->input('path[]',['type' => 'file','multiple'=>'multiple']);

在控制器中,保存方法如下:

// $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.'));
    }
}
于 2016-12-04T15:37:13.700 回答