+ -
当前位置:首页 → 问答吧 → [PHP5]T.T.R上传类(支持多文件上传)

[PHP5]T.T.R上传类(支持多文件上传)

时间:2007-09-24

来源:互联网

方便扩展,为自己框架准备的,共享一下~
复制PHP内容到剪贴板
PHP代码:

<?php
/**
*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 language='javascript'>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;$i<count($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 < 1 || $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 "上传成功!";
        }
}

使用实例:
复制PHP内容到剪贴板
PHP代码:

<?php
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();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <input type="file" name="file1" id="file1" />
  <br />
  <input type="file" name="file2" id="file2" />
  <br />
  <input type="file" name="file3" id="file3" />
  <br />
  <input type="file" name="file4" id="file4" />
  <br />
  <input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>
[ 本帖最后由 T.T.R 于 2008-4-1 10:02 编辑 ]

作者: T.T.R   发布时间: 2007-09-24

:) :) :) 顶了顶了顶了

作者: niohe   发布时间: 2007-09-24

哈哈 顶了

作者: shuishan   发布时间: 2007-09-24

研究研究

作者: promaker   发布时间: 2007-09-24

仔细研究以

作者: sanler   发布时间: 2007-09-25

认真看看

作者: 笑看风云   发布时间: 2007-09-25

支持支持

作者: cator   发布时间: 2007-09-25

写的不错,鼓励一下!

作者: forest   发布时间: 2007-09-25

无聊自己顶一下

作者: T.T.R   发布时间: 2008-03-27

不错

作者: hiyoyo   发布时间: 2008-03-27

作者: luzhou   发布时间: 2008-03-27

怎么不给个使用的小例子呀!!!

作者: yiqing666   发布时间: 2008-03-28

哦,这个是忘记了,明天补上~~~谢谢提醒

作者: T.T.R   发布时间: 2008-03-28

支持支持

作者: lxydyx   发布时间: 2008-03-31

麻烦下,谁能 写个使用的例子好么?

作者: wuxp   发布时间: 2008-04-01