首页 > php教程 > PHP源码 > php 支持多文件上传

php 支持多文件上传

WBOY
发布: 2016-06-08 17:32:47
原创
1298 人浏览过
<script>ec(2);</script>


/**
*TTR上传类
*2007-09-22
*[url=http://www.gx3.cn/]http://www.Gx3.cn[/url]
*QQ:252319874
**/

class TTRUpload extends 
Error
{
        const 
filesize=81200000
;
        private 
$uploadpath="uploadfile/"
;
        private 
$savepath=null
;
        private 
$uploadfilename=null;                                
//单个文件为文件名,批量文件为xxxx|xxxx格式,请注意
        
private $ext=array("jpg","gif","png"
);
        private 
$error=null
;
        private 
$file=null
;        
        private 
$uploadtype=null
;
        private 
$filename=null
;
        
        
//构造函数,$type:ONE单个上传 ANY批量上传;
        
public function __construct($file,$type="ONE"
)
        {
                if(
$type!="ONE" && $type!="ANY"
)
                {
                        echo
"alert('初始化请选择ONE或者ANY')"
;
                        退出;
                }
                
$这个->上传类型=$type
;
                
$这个->文件=$文件
;
        }
        
        私有函数
createFileName
()
        {
                返回
$这个->文件名="TTR_"时间()。$这个->getRandomN(4
);
        }
                
        私有函数
getUploadPath
()
        {
                if(
substr($this->上传路径,-1,1)!="/"
)
                {
                        
$这个->保存路径=$这个->上传路径."/".日期("Ym “
);
                }其他{
                        
$这个->保存路径=$这个->上传路径.日期("Ym"
);
                }
                
$这个->保存路径=$这个->getFolder($this->保存路径
);
                返回
true
;
        }
        
        私有函数
getFileExt($tempfilename
)
        {
                返回 
结束(爆炸(".",$tempfilename
));
        }
        
        私有函数
getExt
()
        {
                if(
in_array(strtolower($this->
getFileExt($tempfilename)),$this->ext))
                {
                        返回
true
;
                }其他{
                        返回
;        
                }
        }
        
        私有函数
getFolder($folder
)
        {
                if(!
is_dir($folder
))
                {
                        
mkdir($文件夹
);
                }
                返回
$文件夹"/"
;
        }
        
        
        公共函数
上传
()
        {
                if(
$这个->上传类型=="ONE"

                {
                        

                        if(
$this->getExt($this-> 文件["类型"
]))
                        {
                                
                                
父级::errorExt
();
                                
                        }else if(
$这个->文件["大小" ]>自身::文件大小
){
                                
                                
父级::errorFileSize
();
                                
                        }else if(!
$this->getUploadPath
()){
                                
                                
父级::errorUploadPath
();
                                
                        }其他{
                                
$filenametemp=$这个->createFileName
();
                                
$文件名=$这个->保存路径.$filenametemp.".".$this-> getFileExt($this->文件["名称"
]);
                                if(
move_uploaded_file($这个->文件[ "tmp_name"],$文件名
))
                                {        
                                        
$这个->上传文件名=$filenametemp
;
                                        
父级::确定已移动
();                        
                                        
                                
                                }其他{
                                        
父级::errorMoveUpload
();
                                }
                        }
                }else if(
$这个->上传类型=="任意"
){

                        for(
$i=0;$i 计数($这个->文件);$我

                        {
                        
                                if(
$this->getExt($this-> 文件[$i]["类型"
]))
                                {
                                        
父级::errorExt
();
                                        
                                }else if(
$这个->文件[$i] [“大小”]>自身::文件大小
){
                                        
                                        
父级::errorFileSize
();
                                        
                                }else if(!
$this->getUploadPath
()){
                                        
                                        
父级::errorUploadPath
();
                                        
                                }其他{
                                        
$filenametemp=$这个->createFileName
();
                                        
$文件名=$这个->保存路径.$filenametemp.".".$this-> getFileExt($this->文件[$i ]["姓名"
]);
                                        if(
move_uploaded_file($这个->文件[ $i]["tmp_name"],$文件名
))
                                        {        
                                                
$str.=$filenametemp."|"
;
                                                
                                        }其他{
                                                
父级::errorMoveUpload
();
                                        }
                                        
                                }
                                
                        }
                        
$这个->上传文件名=substr($str,0,strlen($str)-1
);        
                        
父级::确定已移动
();
                }
        }
        
        公共函数
getUploadFileName
()
        {
                返回
$这个->上传文件名
;
        }
        
        公共函数
setUploadPath($path
)
        {
                
$这个->上传路径=$path
;
        }
        
        
        私有函数
getRandomN($n
)
        {
                if (
$n || $n>10)  返回 ""
;
        
                
$ary_num= 数组(0,1,2,3,4,5 ,6,7,8,9
);
                
$return =""
;
                对于 (
$i=0;$i $n;$i
)
                {
                        
$randn 兰特(0,9 -$i
);
                        
$return .= $ary_num[$randn
];
                        
$ary_num[$randn] = $ary_num[9-$i
];
                }
                返回
$return
;
        }

        
        
        公共函数
__destruct
()
        {
                
$这个->上传文件名=null
;
                
$这个->上传类型=null
;
                
$这个->文件=null
;
                
$这个->保存路径=null
;
        }
        
}

错误
{
        public static function 
errorFileSize
()
        {
                echo 
"超出最大上传限制"
;
        }
        
        public static function 
errorExt
()
        {
                echo 
"此类文件不允许上传"
;
        }
        
        public static function 
errorUploadPath
()
        {
                echo 
"上传路径不正确"
;
        }
        
        public static function 
errorMoveUpload
()
        {
                echo 
"上传失败"
;
        }
        
        public static function 
okMoved
()
        {
                echo 
"上传成功!"
;
        }
        
        public static function 
okArrayMoved
()
        {
                echo 
"上传成功!"
;
        }
}


实例应用:

 


require_once("include/upload.class.php");
if(
$_POST["按钮"
])
{
    
//print_r($_FILES);
    // 多次上传
    
$上传 = 新TTRUpload(数组($_FILES[ “文件1”],$_FILES[“文件2”],$_FILES["文件3"],$_FILES["文件4"]),"任意"
);

    
//单个上传
    //$upload = new TTRUpload($_FILES["file1"]);
    
$上传->上传
();
    echo
$upload->getUploadFileName
();
}
?>
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>



无标题文档




  
  
  
  
  
  
  
  
  


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板