【问题标题】:Actual image not saving to directory实际图像未保存到目录
【发布时间】:2018-07-27 23:58:23
【问题描述】:
  1. 如果用户名输入john,用户名输入将创建一个目录,它将创建一个john

  2. 图片将上传到 webroot\img\users\john\uploaded1.jpg

users 表有目标字段。

它将在数据库中插入正确的目标路径 D:\xampp\htdocs\sample\webroot\img\users\john\uploaded1.jpg

但是没有添加upload1.jpg,下面的警告不能移动文件

警告 (2): move_uploaded_file(D:\xampp\htdocs\sample\webroot\img\users\james\uploaded1.jpg): 无法打开流: 没有这样的文件或目录 [APP/Controller\UsersController.php ,第 584 行] 警告 (2): move_uploaded_file() [https://secure.php.net/function.move-uploaded-file'>function.move-uploaded-file]: 无法移动 'D:\xampp-for-cakephp3\ tmp\php3FD6.tmp' 到 'D:\xampp\htdocs\sample\webroot\img\james\uploaded1.jpg' [APP/Controller\UsersController.php,第 584 行]

<?php

public function register()
{       
      $user = $this->Users->newEntity();

      $value = $this->request->getData('username');  // holds the username input

      if($this->request->is('post')) {

            $image_name        = $this->request->data['profile_pic']['name'];
            $image_tmp         = $this->request->data['profile_pic']['tmp_name'];


            $destination       = WWW_ROOT.'img'.DS.'users'.DS.$value.DS.$image_name;

            //debug($destination);exit;

              move_uploaded_file($image_tmp, $destination);
              $this->request->data['profile_pic'] = $image_name;
              $this->request->data['destination'] = $destination;


             $user= $this->Users->patchEntity($user,$this->request->getData());
             if($this->Users->save($user)) {

                 $dir = new Folder();

                 $path_data = $dir->create(WWW_ROOT.'img'.DS.'users'.DS.$value,true,0777); 

                 $this->Flash->success(__('User profile successfuly  updated.'));

                return $this->redirect(['action' => 'login']);
          } else {
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
        }

        $this->set(compact('user'));
        $this->set('_serialize', ['user']);
    } 
?>

【问题讨论】:

  • 您是否保存了一个冒名顶替者而不是真正的冒名顶替者?对不起,我不得不问。
  • 你是什么意思 tmp ?
  • 开个玩笑,对不起,无论如何,这个文件夹是否存在并且是否可写img\james我不使用Cake,但它很可能是文件权限问题,或者至少是这样是我的首选。这几乎是在说failed to open stream: No such file or directory aka 确保你没有使用 root 来创建它,实际上我今天在 wordpress 网站上做了类似的事情......哈哈

标签: php cakephp cakephp-3.0


【解决方案1】:

此错误是因为该文件夹不存在,因此要克服此错误,您必须先使用 USER INPUT 创建文件夹。

考虑您的示例,用户输入是 John

$value = $this->request->getData('username');    //User input
$image_name = $this->request->data['profile_pic']['name'];
$image_tmp = $this->request->data['profile_pic']['tmp_name'];


$user_input_folder = WWW_ROOT.'img'.DS.'users'.DS.$value.DS;
$destination = $user_input_folder.$image_name;

if (!file_exists($user_input_folder)) {   //Checks file or folder exist or not


    if(mkdir($filePath, 0777, true)){
         $oldMask = umask(0);
         chmod($filePath, 0777);
         umask($oldMask);

         //Then do your file upload
         echo $destination;
         var_dump(move_uploaded_file($image_tmp, $destination));die;
     } else {
           pr("User input contains at least one illegal character, so can't create folder.");die;
     }
}

【讨论】:

  • 我尝试复制你的程序代码,但它没有复制到我自己,我应该把你的代码放在下面的哪里 if($this->request->is('post')) { your代码.....}或低于这个 if($this->Users->save($user)) { your code..... }
  • 实际上我的代码将创建一个目录,但实际图像不会上传到该文件夹​​...但它会保存到具有字段的数据库,目的地
  • if($this->request->is('post')) { your code..... } 使用这个并让我知道 move_uploaded_file 的返回,我已经更新了代码,这样你就可以看到返回了。
  • 它将打印带有确切图像的确切目录..但不会插入数据库中
  • 我尝试取消注释 var_dump 并死掉;但做不到
猜你喜欢
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2015-09-05
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多