其实网上已经有很多这样的类了,不过出于练手的目的还是自己仿照着写了一个。
下面的代码放在一个名为UploadFile.class.PHP文件内
PHP;">
/**
* 构造函数,初始化
* @param string $rand_name 是否随机命名
* @param string $save_path 文件保存路径
* @param string $allow_type 允许上传类型
$allow_type可为数组 array('jpg','jpeg','png','gif');
$allow_type可为字符串 'jpg|jpeg|png|gif';中间可用' ',',';','|'分割
*/
public function __construct($rand_name=true,$save_path='./upload/',$allow_type=''){
$this->rand_name = $rand_name;
$this->save_path = $save_path;
$this->allow_type = $this->get_allow_type($allow_type);
}
/**
* <a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>
* 在<a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>前要做的工作
* (1) <a href="https://www.f2er.com/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>所有信息
* (2) 判断<a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>是否合法
* (3) 设置<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>存放路径
* (4) 是否<a href="https://www.f2er.com/tag/zhongmingming/" target="_blank" class="keywords">重命名</a>
* (5) <a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>完成
* @param array $file <a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>
* $file须包含$file['name'],$file['size'],$file['error'],$file['tmp_name']
*/
public function upload_file($file){
//$this->file = $file;
$this->file_name = $file['name'];
$this->file_size = $file['size'];
$this->error = $file['error'];
$this->file_tmp_name = $file['tmp_name'];
$this->ext = $this->get_file_type($this->file_name);
switch($this->error){
case 0: $this->msg = ''; break;
case 1: $this->msg = '超出了<a href="https://www.f2er.com/tag/PHP/" target="_blank" class="keywords">PHP</a>.ini中<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>大小'; break;
case 2: $this->msg = '超出了MAX_FILE_SIZE的<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>大小'; break;
case 3: $this->msg = '<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>被部分<a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>'; break;
case 4: $this->msg = '没有<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>'; break;
case 5: $this->msg = '<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>大小为0'; break;
default: $this->msg = '<a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>失败'; break;
}
if($this->error==0 && is_uploaded_file($this->file_tmp_name)){
//检测<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>类型
if(in_array($this->ext,$this->allow_type)==false){
$this->msg = '<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>类型不正确';
return false;
}
//检测<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>大小
if($this->file_size > $this->max_size){
$this->msg = '<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>过大';
return false;
}
}
$this->set_file_name();
$this->uploaded = $this->save_path.$this->new_name;
if(move_uploaded_file($this->file_tmp_name,$this->uploaded)){
$this->msg = '<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>成功';
return true;
}else{
$this->msg = '<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>失败';
return false;
}
}
/**
* 设置<a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>后的<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>名
* 当前的毫秒数和原扩展名为新<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>名
*/
public function set_file_name(){
if($this->rand_name==true){
$a = explode(' ',microtime());
$t = $a[1].($a[0]*1000000);
$this->new_name = $t.'.'.($this->ext);
}else{
$this->new_name = $this->file_name;
}
}
/**
* <a href="https://www.f2er.com/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>类型
* @param string $filename 目标<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>
* @return string $ext <a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>类型
*/
public function get_file_type($filename){
$ext = pathinfo($filename,PATHINFO_EXTENSION);
return $ext;
}
/**
* <a href="https://www.f2er.com/tag/huoqu/" target="_blank" class="keywords">获取</a>可<a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>的类型
*/
public function get_allow_type($allow_type){
$s = array();
if(is_array($allow_type)){
foreach($allow_type as $value){
$s[] = $value;
}
}else{
$s = preg_split("/[\s,|;]+/",$allow_type);
}
return $s;
}
//<a href="https://www.f2er.com/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.f2er.com/tag/cuowu/" target="_blank" class="keywords">错误</a>信息
public function get_msg(){
return $this->msg;
}
}
?>
其实上面的代码中还有一个可以改进的地方,就是将那些以‘file_'开头的变量缩写为一个$file数组,这样感觉更好一些。
下面我们来测试一下上面的代码。我在一个名为upfile.PHP文件写测试代码,同时将UploadFile.class.PHP放在同一个路径下。
"; }else{ $file = $_FILES['file'];
$upload = new UploadFile(true,'./images/',array('jpg','png'));
$upload->upload_file($file);
echo $upload->get_msg();
}
}else{
?>
<form action="" method='post' enctype="multipart/form-data">