CakePHP3-如何同时上传两个图像文件?

CakePHP3-如何同时上传两个图像文件?,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我试图一次上传两个图像,我的特定图像文件保存在特定文件夹中,但由于某些原因,正确的图像没有显示出来。在两次上传和显示时,只有第一张图像被复制。有办法解决这个问题吗 public function add() { $teamproject = $this->Teamproject->newEntity(); if ($this->request->is('post')) { if (!empty($this->request-&

我试图一次上传两个图像,我的特定图像文件保存在特定文件夹中,但由于某些原因,正确的图像没有显示出来。在两次上传和显示时,只有第一张图像被复制。有办法解决这个问题吗

 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']);
}
我认为这就是问题发生的地方

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

您需要在保存图像时运行循环,并在图像名称的末尾连接循环索引。问题是你的时间()和mt_rand()返回相同的值。PHP不是这样工作的。您需要附加循环索引,这可能会解决您的问题。

您的意思是像foreach循环一样?到底是怎么回事?我对用php编程真的很陌生。这就是为什么,是的。Foreach将完成这项工作。声明一个值为1的“迭代器”变量,并在包含图像的数组上运行foreach循环。将迭代器与图像名称连接起来,最后是迭代器变量。您也可以使用for循环。