Home > Backend Development > PHP Tutorial > PHP multi-file upload classic code_PHP tutorial

PHP multi-file upload classic code_PHP tutorial

WBOY
Release: 2016-07-13 10:58:23
Original
960 people have browsed it


require_once("include/upload.class.php");
if($_POST["button"])
{
        //print_r($_FILES);
        //多个上传
        //$upload = new TTRUpload($_FILES,"ANY");//同下

        $upload = new TTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");

        //单个上传
        //$upload = new TTRUpload($_FILES["file1"]);
        $upload->upload();
        echo $upload->getUploadFileName();
}
?>




Untitled Document



 
 

 
 

 
 

 
 

 


class TTRUpload extends Error
{
        const filesize=81200000;
         private $uploadpath="uploadfile/";
         private $savepath=null;
private $uploadfilename=null; //A single file is the file name, and the batch file is in xxxx|xxxx format, please note
        private $ext=array("jpg","gif","png");
         private $error=null;
         private $file=null;                                                                   private $uploadtype=null;
         private $filename=null;
         
//Constructor, $type:ONE single upload ANY batch upload;
        public function __construct($file,$type="ONE")
           {
If($type!="ONE" && $type!="ANY")
                                                 {
                                     echo "";
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;$ifile);$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 "Maximum upload limit exceeded";
}
         
         public static function errorExt()
           {
echo "Such files are not allowed to be uploaded";
}
         
         public static function errorUploadPath()
           {
echo "The upload path is incorrect";
}
         
         public static function errorMoveUpload()
           {
echo "Upload failed";
}
         
         public static function okMoved()
           {
echo "Upload successful!";
}
         
         public static function okArrayMoved()
           {
echo "Upload successful!";
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632005.htmlTechArticle?php require_once(include/upload.class.php); if($_POST[button]) { // print_r($_FILES); //Multiple uploads//$upload = new TTRUpload($_FILES,ANY);//Same as below $upload = new TTRUpload(array($...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template