【问题标题】:windows 10 file upload and permissions phpwindows 10文件上传和权限php
【发布时间】:2017-01-19 15:10:20
【问题描述】:

我在移动上传的文件时遇到问题,问题似乎是权限问题,非常感谢任何帮助。我正在使用 Windows 10,我已将 php.ini 中的临时文件夹更改为 xampp 目录中的自定义文件夹,当我检查此临时文件夹时,该文件也不存在,所以它似乎甚至不是上传给他们临时。请帮忙。

class PortfolioItem extends DataBaseCommonObj{

protected static $table_name = 'portfolio_item';
protected static $db_fields = array('id','filename','mimetype','size','caption','job_id');

public $id;
public $filename;
public $mimetype;
public $size;
public $caption;
public $job_id;


private $temp_path;
protected $upload_dir="_portfolio-items";
public $errors=array();

protected $upload_errors = array(
  // http://www.php.net/manual/en/features.file-upload.errors.php
  UPLOAD_ERR_OK                 => "No errors.",
  UPLOAD_ERR_INI_SIZE   => "Larger than upload_max_filesize.",
  UPLOAD_ERR_FORM_SIZE  => "Larger than form MAX_FILE_SIZE.",
  UPLOAD_ERR_PARTIAL        => "Partial upload.",
  UPLOAD_ERR_NO_FILE        => "No file.",
  UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
  UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
  UPLOAD_ERR_EXTENSION  => "File upload stopped by extension."
);

public function attach_file($file){
  global $session;
  if(!$file || empty($file) || !is_array($file)){
    $this->errors[] = 'no file was uploaded';
    return false;
  }elseif ($file['error'] != 0) {
    $this->errors[]=$this->upload_errors[$file['error']];
    return false;
  }else {
    $this->filename = basename($file['name']);
    $this->temp_path = $file['tmp_name'];
    $this->mimetype = $file['type'];
    $this->size = $file['size'];

    if(isset($session->message) && $session->message !== ''){
      $current_job = Job::find_by_id(intval($session->message));
      $this->job_id = $current_job->id;
    }
    return true;
  }
}


defined('DS')?null:define('DS',DIRECTORY_SEPARATOR);
defined('SITE_ROOT')?null:define('SITE_ROOT', DS.'mp_creations');
defined('INC_PATH')?null:define('INC_PATH', SITE_ROOT.DS.'includes');
defined('PUB_PATH')?null:define('PUB_PATH', SITE_ROOT.DS.'public');


public function save(){
  if(isset($this->id)){
    $this->update();
  }else{
    if(!empty($this->errors)){
      return false;
    }

    if(empty($this->filename) || empty($this->temp_path)){
      $this->errors[] = 'file path not available';
      return false;
    }

    $target_path = PUB_PATH.DS.$this->upload_dir.DS.$this->filename;

    if(file_exists($target_path)){
      $this->errors[] = "the file {$this->filename} already exists";
      return false;
    }

    if(move_uploaded_file($this->temp_path,$target_path)){
      if($this->create()){
        unset($this->temp_path);
        return true;
      }
    }else {
      $this->errors[]='The file upload failed, possibly due to permission issues';
      return false;
    }
  }
}

【问题讨论】:

  • 请提供代码。没有它,它只是一个魔法
  • 据我所知,临时文件应在请求结束时删除。您可以在 move_uploaded_file 之后设置 sleep() 并检查临时文件夹中的文件。

标签: php windows file permissions ownership


【解决方案1】:

问题似乎出在我的方法上,与权限无关。我正在使用存储在会话中的值传递给 move_uploaded_file() 函数,一旦我直接从 POST 请求中完成它就可以正常工作。是否需要从 POST 请求中调用 move_uploaded_file() ?任何人都想对此有所了解,请按照我想了解幕后发生的事情的方式进行。

【讨论】:

    【解决方案2】:

    如果文件没有被移走或重命名,则该文件将在请求结束时从临时目录中删除。

    http://php.net/manual/en/features.file-upload.post-method.php
    

    【讨论】:

    • 很酷,但它没有被移动,因为它不在目标文件夹中
    • 当你的 $this->temp_path 设置好后,给我们这段代码
    • 编辑以显示设置临时路径,它来自发布请求中的 $_FILES
    • 这里的任何帮助都会是很棒的人。再次感谢。
    猜你喜欢
    • 2011-03-07
    • 2013-02-05
    • 2012-10-21
    • 2016-05-04
    • 2016-08-25
    • 2015-10-15
    • 2011-04-08
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多