|
|

晚上写的很简单的一个文件上传类。没什么特别功能,除了类的设计上有点可取把。
功能:支持单文件、多文件上传。
本来想用pear的http_upload,这个很久没更新了,而且写的不是特别好。所以早就写了个。顺便看了下自己以前用PHP5写的一个多文件上传的类,发现写的好烂。。汗。。
这个也是刚刚写的,可能还存在一些未知的错误,到时候发现再修改了。
代码如下:
<?
/**
* filename:upload.class.php
* since:2006-11-28
* description:文件上传类。
* author feifengxlq<许立强> http://www.phpobject.net/blog
* version v0.02
* demo:
$upload=new upload(array('path'=>'../test','allow_type'=>array('txt')));
print_r($upload->upfile('userfile'));
自定义上传文件名
$upload=new upload();
print_r($upload->upfile('userfile',array('allow_type'=>array('txt'),'filename'=>'test.txt')));
*/
class upload
{
var $path='/uploadfiles/';//上传文件的路径
var $allow_type=array('jpg','gif');//允许上传的文件类型
var $max_size='2048';//允许的最大文件大小
//var $field='uploadfile';//上传文件的字段名
var $overwrite=false;//是否设置成覆盖模式
var $renamed=true;//是否直接使用上传文件的名称,还是系统自动命名
//private
var $upfile=array();//上传后文件信息
var $ext='';//上传文件的扩展名
var $errormsg='';//出错信息
var $filename='';//是否设置了上传后的文件名
/**
* 出错模式
* 0:出错就exit
* 1:出错返回出错信息
*/
var $error_mode=0;//出错模式
function upload($options=array())
{
//构造
$this->_set_options($options);
}
//上传文件
function upfile($field,$options=array())
{
//设置上传文件的属性
$this->_set_options($options);
//获取文件
$this->_set_upfile($field);
//检查文件
$this->_check();
//设置上传后的文件名
$this->_set_filename();
//上传文件
if(@move_uploaded_file($this->upfile['tmp_name'],$this->upfile['filename']))return $this->upfile;
//上传失败
$this->error(__FUNCTION__.'():'.$this->upfile['filename'].'上传失败。');
return false;
}
//获取出错信息
function get_error()
{
if($this->errormsg)return $this->errormsg;
return false;
}
/**-----------private-----------------------------------------------*/
/**
* 设置上传后的文件名
*/
function _set_filename()
{
//是否重命名
if($this->filename){
//如果用户设置了文件名
if(file_exists($this->filename))@unlink($this->filename);
$this->upfile['filename']=$this->filename;
return true;
}
//检查上传文件路径
if(!file_exists($this->path))$this->_mkpath($this->path);
if($this->path[strlen($this->path)-1]!='/')$this->path.='/';//
if($this->renamed){
$ext=$this->ext;
if(empty($ext))$ext=$this->_get_ext($this->upfile['name']);
$filename=uniqid(time()).'.'.$ext;
}else{
$filename=$this->upfile['name'];
}
//是否覆盖模式
if(!$this->overwrite){
//不覆盖,检查是否存在同名文件
$i=1;
while(file_exists($this->path.$filename)){
$filename=$i.'_'.$filename;
$i++;
}
}else{
//覆盖模式,如果存在改文件,则删除
if(file_exists($this->path.$filename))@unlink($this->path.$filename);
}
$this->upfile['filename']=$this->path.$filename;
}
/**
* 检查文件是否符合需要
*/
function _check()
{
//检查文件大小
$this->_check_size();
//检查文件类型
$this->_check_type();
}
/**
* 设置属性
*/
function _set_options($options){
if(is_array($options)){
foreach($options as $key=>$value){
if(in_array($key,array('path','allow_type','max_size','overwrite','renamed','error_mode','filename'))){
$this->_set($key,$value);
}
}
}
}
/**
* 检查文件类型getimagesize
*/
function _check_type()
{
$this->_get_ext($this->upfile['name']);
if(in_array($this->ext,$this->allow_type))
{
//检查mine类型
$file_mine=$this->upfile['type'];
if(empty($file_mine)){
//
if(extension_loaded('gd')){
if($rs=getimagesize($this->upfile['name'])){
$file_mine=$rs['mime'];
}
}
}
if(empty($file_mine))return true;
//检查mine类型是否符合
$this->_check_mine($file_mine);
}else{
$this->error(__FUNCTION__.'():'.$this->upfile['name'].'文件类型'.$this->ext.'不符合。只允许上传'.implode(',',$this->allow_type));
}
}
/**
* 检查上传文件的mine类型
*/
function _check_mine($mine)
{
require_once('filetype.php');
$pass=false;
foreach($this->allow_type as $type){
if(is_array($filetype[$type])){
if(in_array($mine,$filetype[$type])){
//
$pass=true;
break;
}
}elseif($filetype[$type]==$mine){
$pass=true;
break;
}
}
if(!$pass)$this->error(__FUNCTION__.'():'.$this->upfile['name'].'文件mine类型不符合。只允许上传'.implode(',',$this->allow_type));
}
/**
* 获取文件的后缀
*/
function _get_ext($file){
$ext = explode(".", $file);
$ext = $ext[count($ext) - 1];
$this->ext=strtolower($ext);
return $this->ext;
}
/**
* 检查文件大小
*/
function _check_size()
{
if($this->upfile['size']>$this->max_size*1024)
$this->error(__FUNCTION__.'():'.$this->upfile['name'].'文件大小超过了限制'.$this->max_size.'MB');
}
/**
* 获取文件
* @param field
* @return
*/
function _set_upfile($field)
{
if(!$_FILES[$field])$this->error(__FUNCTION__.'():文件不存在!');
$this->upfile=$_FILES[$field];
}
function _set($key,$value){
$this->$key=$value;
}
/**
* 构造目录
*/
function _mkpath($path,$mode=700){
$path=str_replace("\\","/",$path);
$path_info=explode('/',$path);
$total=count($path_info);
$path='';
for($i=0;$i<$total;$i++)
{
$path.=$path_info[$i].'/';
if(empty($path_info[$i]))continue;
if(file_exists($path))
{
continue;
}else{
if(!@mkdir($path,$mode))$this->error(__FUNCTION__.'(),创建'.$path.'出错!');
}
}
return true;
}
/**
* 格式化文件大小
*/
function format_size($size)
{
if($size<1024){
return $size.'B';
}elseif ($size<1024*1024){
return number_format((double)($size/1024),2).'KB';
}else{
return number_format((double)($size/(1024*1024)),2).'MB';
}
}
/**
* 出错处理!
*/
function error($msg)
{
if($this->error_mode==0)die('ERROR : file '.__FILE__.' function '.$msg);
$this->errormsg.='ERROR : file '.__FILE__.' function '.$msg."\n";
}
}
?>
提供一个简单的demo
<?
if(empty($_FILES)){
?>
<form enctype="multipart/form-data" action="" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
<? }else{
require_once('../libs/classes/upload.class.php');
$upload=new upload(array('path'=>'../test','allow_type'=>array('txt')));
print_r($upload->upfile('userfile'));
}
?>
另外增加一个自定义上传文件名的demo
<?
if(empty($_FILES)){
?>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
<? }else{
require_once('../libs/classes/upload.class.php');
$upload=new upload();
print_r($upload->upfile('userfile',array('allow_type'=>array('txt'),'filename'=>'test.txt')));
}
?> |
|