upload.class.php封装类文件
<?php
class UploadFile
{
private $max_size = '4000000'; //设置上传文件的大小,此为4M
private $rand_name = true; //是否采用随机命名
private $replace_file = false; //是否替换文件
private $allow_type = array(); //允许上传的文件扩展名
private $error = 0; //错误代号
private $msg = ''; //信息
private $new_name = ''; //上传后的文件名
private $prefix_path = ''; //绝对路径前缀
private $save_path = ''; //文件保存路径
private $replace_path = ''; //需要替换的文件路径
private $uploaded = ''; //路径.文件名
private $file = ''; //等待上传的文件
private $file_type = array(); //文件类型
private $file_ext = ''; //上传文件的扩展名
private $file_name = ''; //文件原名称
private $file_size = 0; //文件大小
private $file_tmp_name = ''; //文件临时名称
/**
* 构造函数,初始化
* @param
*/
public function __construct($rand_name = true, $prefix_path = __DIR__, $save_path = "/upload/", $allow_type = '')
{
$this->rand_name = $rand_name;
$this->prefix_path = $prefix_path;
$this->save_path = $save_path;
$this->allow_type = $this->__get_allow_type($allow_type);
}
/**
* 上传文件
* 在上传文件前要做的工作
* (1) 获取文件所有信息
* (2) 判断上传文件是否合法(文件类型,文件大小)
* (3) 设置文件存放路径
* (4) 是否重命名
* (5) 上传完成
* @param array $file 上传文件
* $file须包含$file['name'], $file['size'], $file['error'], $file['tmp_name']
*/
public function upload_file($file, $replace=false, $replace_path='')
{
$this->replace_file = $replace;
$this->replace_path = $replace_path;
$this->file_name = $file['name'];
$this->file_size = $file['size'];
$this->file_tmp_name = $file['tmp_name'];
$this->error = $file['error'];
$this->file_ext = $this->__get_file_type($this->file_name);
switch ($this->error) {
case 0:
$this->msg = '';
break;
case 1:
$this->msg = '上传的文件超过了php.ini中upload_max_filesize选项限制的值';
break;
case 2:
$this->msg = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
$this->msg = '文件只有部分被上传';
break;
case 4:
$this->msg = '没有文件被上传';
break;
case 5:
$this->msg = '文件大小为0';
break;
case 6:
$this->msg = '找不到临时文件夹';
break;
case 7:
$this->msg = '文件写入失败';
break;
default:
$this->msg = '上传失败';
break;
}
if ( $this->error == 0 && is_uploaded_file($this->file_tmp_name) ) {
//检测文件类型
if ( !in_array($this->file_ext, $this->allow_type) ) {
$this->msg = '文件类型不正确';
return false;
}
//检测文件大小
if ( $this->file_size > $this->max_size ) {
$this->msg = '文件过大';
return false;
}
} else {
return false;
}
$this->__set_file_name();
if ( move_uploaded_file($this->file_tmp_name, $this->uploaded) ) {
$this->msg = '文件上传成功';
//上传成功后需要返回的数据,根据自己的业务逻辑来定义
$info = array(
'code' => 'succ',
'msg' => $this->msg,
'url' => $this->save_path.$this->new_name,
'type' => $this->file_ext
);
return $info;
} else {
$this->msg = '文件上传失败';
return false;
}
}
//获取错误信息
public function get_msg(){
return $this->msg;
}
/**
* 设置上传路径和新的文件名
* 当前的毫秒数和原扩展名为新文件名
* 替换时原文件路径不变,防止原文件丢失,所以对其进行重新命名
*/
private function __set_file_name(){
if ( $this->replace_file == true ) {
$this->path = $this->prefix_path.$this->replace_path;
if ( !file_exists($this->path) ) {
$this->replace_file = false;
$this->__set_file_name();
} else {
$a = explode(' ', microtime());
$t = $a[1].($a[0]*1000000);
$old_dir = pathinfo($this->replace_path, PATHINFO_DIRNAME);//文件目录
$old_name = pathinfo($this->replace_path, PATHINFO_FILENAME);//不包含后缀的文件名
$old_ext = pathinfo($this->replace_path, PATHINFO_EXTENSION);//文件后缀
$new_name = $this->prefix_path.$old_dir.'/TH-'.$old_name.'-'.$t.'.'.$old_ext;
if ( rename($this->path, $new_name) ) {
$this->uploaded = $this->prefix_path.$this->replace_path;
} else {
$this->msg = '替换失败';
}
}
} else {
$this->path = $this->prefix_path.$this->save_path;
if ( !file_exists($this->path) ) {
mkdir($this->path);
chmod($this->path, 0777);
}
if( $this->rand_name == true ){
$a = explode(' ', microtime());
$t = $a[1].($a[0]*1000000);
$this->new_name = $t.'.'.($this->file_ext);
}else{
$this->new_name = $this->file_name;
}
$this->uploaded = $this->path.$this->new_name;
}
}
/**
* 获取上传文件的类型
* @param string $file_name 目标文件名
* @return string $ext 文件类型
*/
private function __get_file_type($file_name)
{
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
return $ext;
}
/**
* 获取可允许上传的文件类型
* @param $allow_type 调用类时定义的可上传类型
* @return array $arr 类型数组
*/
private function __get_allow_type($allow_type)
{
$arr = array();
if ( is_array($allow_type) ) {
foreach ($allow_type as $key => $value) {
$arr[] = $value;
}
} else {
$arr = preg_split("/[\s,|;]+/", $allow_type);
}
return $arr;
}
}
?>
form表单提交indexController.php文件
<?php
ini_set('display_errors',1); //错误信息
ini_set('display_startup_errors',1); //php启动错误信息
error_reporting(-1); //打印出所有的 错误信息
if ( isset($_FILES['file']) && !empty($_FILES['file']) ) {
$prefix_path = '/var/www/html/wx/wangxin';
$path = '/upload/201909/';
$replace_path = '/upload/201909/1567738613996940.png';
$ext = array('png','jpg');
//实例化类
$upload = new \UploadFile(true, $prefix_path, $path, $ext);
$arr = array();
$info = array();
foreach ($_FILES['file'] as $key => $value) {
foreach ($value as $k => $val) {
$arr[$k][$key] = $val;
}
}
foreach ($arr as $key => $value) {
$info[$key] = $upload->upload_file($value, true, $replace_path);
}
var_dump($info);
var_dump($upload->get_msg());
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文件上传</title>
</head>
<body>
<form action="" method='post' enctype="multipart/form-data">
<input type="file" name="file[]" id="file" multiple />
<input type="submit" value="上传" name="sub" />
</form>
</body>
</html>