Windows 10文件上传和权限php

时间:2016-09-12 08:12:28

标签: php windows file permissions ownership

我无法移动上传的文件,问题似乎是权限问题,任何帮助都会非常感激。我使用的是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;
    }
  }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

问题似乎是我的方法而不是与权限有关。我当时正在使用存储在会话中的值传递给move_uploaded_file()函数,一旦我直接从POST请求执行它就可以正常工作。是否需要从POST请求调用move_uploaded_file()?任何人都想关注这一点,请做,因为我很想知道幕后发生的事情。