0

I am using cakephp 3.3. I am able to upload single image but it is not getting saved under webroot/uploads. I want to upload multiple images and save it. How can I achieve it? Please provide some inputs. I am very new to PHP programming and this framework.Thanks!

       `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;

}

user2301
  • 1,857
  • 6
  • 32
  • 63

1 Answers1

0

In add.ctp make input field something like:

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

And in controller make save method something like this:

// $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.'));
    }
}
Manohar Khadka
  • 2,186
  • 2
  • 18
  • 30
  • Thanks! I need to loop through '$images' to get each Image details and store in the database. And also save the Image under Webroot\uploads. $images= $this->Images->newEntities($this->request->data); will give me an array of images. But i get index not found when trying to fetch the filename . – user2301 Dec 06 '16 at 22:27
  • @ Manohar Khadka : I tried this way! it did not work. It gives me Unable to find table class entry as error. – user2301 Dec 08 '16 at 12:18