Kasun Wijesekara
Kasun Wijesekara

Reputation: 167

CakePhP 3 - How to upload two image files at the same time?

I'm trying to upload two images at once, my specific image files get saved in the specific folders but for some reason the proper images doesn't show up. Only the first image gets reproduced on both uploads and show up. Is there a way to fix this?

 public function add()
 {
    $teamproject = $this->Teamproject->newEntity();
    if ($this->request->is('post')) {

         if (!empty($this->request->data['mainimg'])) {
        $file = $this->request->data['mainimg'];
        $destinationPath = WWW_ROOT . 'project_img' . DS;
        $filename = $this->request->data['mainimg']['name'];
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        $arr_ext = array('jpg', 'jpeg','png', 'JPG');

        if (!in_array($extension, $arr_ext)) {

            $this->Flash->error(__('Incorrect Image Format.. Please Try Again'));

        }else{

            $uniqueFileName = time().'_'.mt_rand(10000000, 99999999).'_'.mt_rand(10000000, 99999999).'.'.$extension;
            move_uploaded_file($file['tmp_name'], $destinationPath . '/' . $uniqueFileName);
            $filePath = '/project_img/' . $uniqueFileName; 

        }
    }   

    if (!empty($this->request->data['imgone'])) {
        $file = $this->request->data['imgone'];
        $destinationPath = WWW_ROOT . 'imgone_img' . DS;
        $filename = $this->request->data['imgone']['name'];
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        $arr_ext = array('jpg', 'jpeg','png', 'JPG');

        if (!in_array($extension, $arr_ext)) {

            $this->Flash->error(__('Incorrect Image Format.. Please Try Again'));

        }else{

            $uniqueFileName = time().'_'.mt_rand(10000000, 99999999).'_'.mt_rand(10000000, 99999999).'.'.$extension;
            move_uploaded_file($file['tmp_name'], $destinationPath . '/' . $uniqueFileName);
            $filePath = '/imgone_img/' . $uniqueFileName; 

        }
    }

        $teamproject = $this->Teamproject->patchEntity($teamproject, $this->request->data);
        $teamproject->mainimg = $filePath;
        $teamproject->imgone = $filePath;


        if ($this->Teamproject->save($teamproject)) {
            $this->Flash->success(__('The teamproject has been saved.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The teamproject could not be saved. Please, try again.'));
        }


    $users = $this->Teamproject->Users->find('list', ['limit' => 200]);
    $this->set(compact('teamproject', 'users'));
    $this->set('_serialize', ['teamproject']);
}

I think this is the place where the issue occurs

    $teamproject = $this->Teamproject->patchEntity($teamproject, $this->request->data);
    $teamproject->mainimg = $filePath;
    $teamproject->imgone = $filePath;

Upvotes: 2

Views: 96

Answers (1)

codvlpr
codvlpr

Reputation: 122

You need to run a loop while saving the images and concatenate the looping index at the end of the image name. The thing is your time() and mt_rand() is returning the same value. PHP doesn't work that way. You need to append the looping index and that probably will solve your problem.

Upvotes: 2

Related Questions