PHP文件上传类
<?php
namespace systemcore;
/**
* PHP文件上传类
* @author chenshuo
*
*/
class Upload {
// 允许的类型
private $allow_type = array();
// 允许的最大尺寸
private $max_size;
// 上传文件的存储路径
private $path;
// 源文件名称
private $orig_name;
// 源文件大小
private $orig_size;
// 源文件后缀名
private $orig_ext;
// 源文件的MIME类型
private $orig_mime_type;
// 临时文件
private $tmp_name;
// 表单字段名称
private $input_name;
// 错误列表
private $msg_error = array();
// 上传完成后生成的新文件名
public $filename;
// 多文件上传返回数组集合
public $result;
/**
* 构造方法传参
* @access public
* @param $allow_type array 允许上传的文件MIME类型
* @param $max_size int 允许上传的最大尺寸
* @param $path string 文件上传后的存储路径
* @param $input_name string 上传文件在表单中的name值
* @ $upload = new Upload(array('image/gif','image/png', 'image/jpeg', 'image/pjpeg'), 1024, 'upload/', 'file');
* @ $result = $upload->upload();
*/
public function __construct($allow_type, $max_size, $path, $input_name) {
$this->allow_type = $allow_type;
$this->input_name = $input_name;
$this->max_size = $max_size;
$this->path = $path;
$this->error_msg = array(
'path_error' => '[错误]: 确认存在 '.$this->path.' 目录,并已经设置为可写权限',
'perms_error' => '[错误]: 指定上传目录没有写权限',
'empty_error' => '[错误]: 未选择任何文件',
'type_error' => '[错误]: 文件类型不正确',
'size_error' => '[错误]: 文件超过规定大小',
'upload_error' => '[错误]: 由于网络原因导致错误!',
);
}
/**
* 文件上传
* @access public
* @return $$this->result array 返回二维数组,包括文件编号、上传状态、结果(上传成功返回文件名,失败返回错误信息)
*/
public function upload() {
// 检测上传路径是否存在
if(file_exists($this->path)) {
$fileperms = substr(sprintf('%o', fileperms($this->path)), -4);
if($fileperms != '0777') {
$this->result[0]['order'] = 0;
$this->result[0]['status'] = 'faild';
$this->result[0]['msg'] = $this->error_msg['perms_error'];
return $this->result;
} else {
$this->orig_name = $_FILES[$this->input_name]['name'];
$this->orig_size = $_FILES[$this->input_name]['size'];
$this->orig_mime_type = $_FILES[$this->input_name]['type'];
$this->tmp_name = $_FILES[$this->input_name]['tmp_name'];
return $this->execute();
}
} else {
$this->result[0]['order'] = 0;
$this->result[0]['status'] = 'faild';
$this->result[0]['msg'] = $this->error_msg['path_error'];
return $this->result;
}
}
/**
* 执行文件上传
* @access private
* @return $$this->result array 返回二维数组,包括文件编号、上传状态、结果(上传成功返回文件名,失败返回错误信息)
*/
private function execute() {
for($i=0; $i<count($this->orig_name); $i++) {
if(empty($this->orig_name[$i])) {
$this->result[$i]['order'] = $i;
$this->result[$i]['status'] = 'faild';
$this->result[$i]['msg'] = $this->error_msg['empty_error'];
} else {
if(!in_array($this->orig_mime_type[$i], $this->allow_type)) {
$this->result[$i]['order'] = $i;
$this->result[$i]['status'] = 'faild';
$this->result[$i]['msg'] = $this->error_msg['type_error'];
} else {
if(ceil($this->orig_size[$i]/1024) > $this->max_size) {
$this->result[$i]['order'] = $i;
$this->result[$i]['status'] = 'faild';
$this->result[$i]['msg'] = $this->error_msg['size_error'];
} else {
$this->orig_ext = strtolower(substr($this->orig_name[$i], strrpos($this->orig_name[$i], '.')+1));
$this->filename[$i] = date('YmdHis').rand(100,999).".".$this->orig_ext;
$upload_result = move_uploaded_file($this->tmp_name[$i], $this->path.$this->filename[$i]);
if($upload_result) {
$this->result[$i]['order'] = $i;
$this->result[$i]['status'] = 'success';
$this->result[$i]['msg'] = $this->filename[$i];
} else {
$this->result[$i]['order'] = $i;
$this->result[$i]['status'] = 'faild';
$this->result[$i]['msg'] = $this->error_msg['upload_error'];
}
}
}
}
}
return $this->result;
}
}
?>