热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » PHP/Perl编程 » 文件上传类3

文件上传类3

论坛链接
  • 文件上传类3
  • 发布时间:2007-10-04 15:23:21    浏览数:5472    发布者:tznktg    设置字体【   
晚上写的很简单的一个文件上传类。没什么特别功能,除了类的设计上有点可取把。
功能:支持单文件、多文件上传。
本来想用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')));
}
?>
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理