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

PHP multi-file upload classic code_PHP tutorial

Jul 13, 2016 am 10:58 AM
include once php require upload code document classic


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($...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles