【发布时间】: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