首页 > php教程 > PHP源码 > 正文

php 支持多文件上传

php中文网
发布: 2016-06-08 17:32:47
原创
1366人浏览过
<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 
"<script>alert('初始化请选择ONE或者ANY')</script>"
;
                        exit;
                }
                
$this->uploadtype=$type
;
                
$this->file=$file
;
        }
        
        private function 
createFileName
()
        {
                return 
$this->filename="TTR_".time().$this->getRandomN(4
);
        }
                
        private function 
getUploadPath
()
        {
                if(
substr($this->uploadpath,-1,1)!="/"

                {
                        
$this->savepath=$this->uploadpath."/".date("Ym"
);
                }else{
                        
$this->savepath=$this->uploadpath.date("Ym"
);
                }
                
$this->savepath=$this->getFolder($this->savepath
);
                return 
true
;
        }
        
        private function 
getFileExt($tempfilename
)
        {
                return 
end(explode(".",$tempfilename
));
        }
        
        private function 
getExt
()
        {
                if(
in_array(strtolower($this->getFileExt($tempfilename)),$this->ext
))
                {
                        return 
true
;
                }else{
                        return 
false
;        
                }
        }
        
        private function 
getFolder($folder
)
        {
                if(!
is_dir($folder
))
                {
                        
mkdir($folder
);
                }
                return 
$folder."/"
;
        }
        
        
        public function 
upload
()
        {
                if(
$this->uploadtype=="ONE"
)
                {
                        

                        if(
$this->getExt($this->file["type"
]))
                        {
                                
                                
parent::errorExt
();
                                
                        }else if(
$this->file["size"]>self::filesize
){
                                
                                
parent::errorFileSize
();
                                
                        }else if(!
$this->getUploadPath
()){
                                
                                
parent::errorUploadPath
();
                                
                        }else{
                                
$filenametemp=$this->createFileName
();
                                
$filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file["name"
]);
                                if(
move_uploaded_file($this->file["tmp_name"],$filename
))
                                {        
                                        
$this->uploadfilename=$filenametemp
;
                                        
parent::okMoved
();                        
                                        
                                
                                }else{
                                        
parent::errorMoveUpload
();
                                }
                        }
                }else if(
$this->uploadtype=="ANY"
){

                        for(
$i=0;$icount($this->file);$i
++)
                        {
                        
                                if(
$this->getExt($this->file[$i]["type"
]))
                                {
                                        
parent::errorExt
();
                                        
                                }else if(
$this->file[$i]["size"]>self::filesize
){
                                        
                                        
parent::errorFileSize
();
                                        
                                }else if(!
$this->getUploadPath
()){
                                        
                                        
parent::errorUploadPath
();
                                        
                                }else{
                                        
$filenametemp=$this->createFileName
();
                                        
$filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file[$i]["name"
]);
                                        if(
move_uploaded_file($this->file[$i]["tmp_name"],$filename
))
                                        {        
                                                
$str.=$filenametemp."|"
;
                                                
                                        }else{
                                                
parent::errorMoveUpload
();
                                        }
                                        
                                }
                                
                        }
                        
$this->uploadfilename=substr($str,0,strlen($str)-1
);        
                        
parent::okMoved
();
                }
        }
        
        public function 
getUploadFileName
()
        {
                return 
$this->uploadfilename
;
        }
        
        public function 
setUploadPath($path
)
        {
                
$this->uploadpath=$path
;
        }
        
        
        private function 
getRandomN($n
)
        {
                if (
$n || $n>10)  return ""
;
        
                
$ary_num= array(0,1,2,3,4,5,6,7,8,9
);
                
$return =""
;
                for (
$i=0;$i$n;$i
++)
                {
                        
$randn rand(0,9-$i
);
                        
$return .= $ary_num[$randn
];
                        
$ary_num[$randn] = $ary_num[9-$i
];
                }
                return 
$return
;
        }

        
        
        public function 
__destruct
()
        {
                
$this->uploadfilename=null
;
                
$this->uploadtype=null
;
                
$this->file=null
;
                
$this->savepath=null
;
        }
        
}

class 
Error
{
        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["button"
])
{
    
//print_r($_FILES);
    //多个上传
    
$upload = new TTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY"
);

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



Untitled Document




  
  

  
  

  
  

  
  

  


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号