php - CakePhP 3 - 如何同时上传两个图像文件?

标签 php cakephp cakephp-3.0

我试图一次上传两个图像,我的特定图像文件保存在特定文件夹中,但由于某种原因,正确的图像没有显示。只有第一张图像会在两次上传和显示中重现。有没有办法来解决这个问题?

 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;

最佳答案

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

关于php - CakePhP 3 - 如何同时上传两个图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42883931/

相关文章:

php - 查询 SQL 无法与 Laravel 5 Controller 一起使用

php - CakePHP 3.0 数据库修改

mysql - CakePHP - 驻留在不同数据库上且具有不同查询语言的表之间的 $hasAndBelongsToMany 和 $hasMany 关系?

CakePHP 全局 slug 路由器

php - CakePHP 3 - 创建新的验证器

php - 每个案例使用多个值进行 PHP 切换的最佳方法是什么?

php - 通过 id 从 mysql 中提取多行的最有效方法是什么?

php - 拉维尔 5.4 : Api route list

sqlite - SQLite:如何将默认输入类型设置为 `text`而不是 `textarea`

CakePHP3 : How can I do text searching using full-text indexes