Windows 10文件上传和权限php



我在移动上传的文件时遇到了麻烦,问题似乎是权限问题,任何帮助都会非常感激。我使用的是windows 10,我已经将php.ini中的临时文件夹更改为xampp目录中的自定义文件夹,当我检查此临时文件夹时,该文件也不存在,所以似乎它甚至没有被上传到他们temp.请帮助。

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;
    }
  }
}

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

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

问题似乎是我的方法,而不是与权限有关。我正在使用存储在会话中的值传递给move_uploaded_file()函数,一旦我直接从POST请求中执行,它就工作得很好。是move_uploaded_file()需要从POST请求调用吗?如果有人想解释一下这个问题,请这样做,因为我很想知道幕后发生了什么。

最新更新