前幾天看了一本關於PHP的書,讓我感觸很深,我先介紹一下PHP的發展史,然後在教大家一個PHP上傳多個文件的一個小技巧。讓我們先來簡單的介紹一下PHP吧!PHP 最初是1994年Rasmus Lerdorf創建的,剛剛開始只是一個簡單的用Perl語言編寫的程序,用來統計他自己網站的訪問者。後來又用C語言重新編寫,包括可以訪問數據庫。
PHP上傳多個文件代碼實現:
- <?php
- require_once("include/upload.class.php");
- if($_POST["button"])
- {
- //print_r($_FILES);
- //多個上傳
- //$upload=newTTRUpload($_FILES,"ANY");//同下
- $upload=newTTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");
- //單個上傳
- //$upload=newTTRUpload($_FILES["file1"]);
- $upload->upload();
- echo$upload->getUploadFileName();
- }
- ?>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
- <title>UntitledDocument</title>
- </head>
- <body>
- <formactionformaction=""method="post"enctype="multipart/form-data"name="form1"id="form1">
- <inputtypeinputtype="file"name="file1"id="file1"/>
- <br/>
- <inputtypeinputtype="file"name="file2"id="file2"/>
- <br/>
- <inputtypeinputtype="file"name="file3"id="file3"/>
- <br/>
- <inputtypeinputtype="file"name="file4"id="file4"/>
- <br/>
- <inputtypeinputtype="submit"name="button"id="button"value="Submit"/>
- </form>
- </body>
- </html>
- <?php
- classTTRUploadextendsError
- {
- constfilesize=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批量上傳;
- publicfunction__construct($file,$type="ONE")
- {
- if($type!="ONE"&&$type!="ANY")
- {
- echo"<scriptlanguagescriptlanguage='javascript'>alert('初始化請選擇ONE或者ANY')</script>";
- exit;
- }
- $this->uploadtype=$type;
- $this->file=$file;
- }
- privatefunctioncreateFileName()
- {
- return$this->filename="TTR_".time().$this->getRandomN(4);
- }
- privatefunctiongetUploadPath()
- {
- 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);
- returntrue;
- }
- privatefunctiongetFileExt($tempfilename)
- {
- returnend(explode(".",$tempfilename));
- }
- privatefunctiongetExt()
- {
- if(in_array(strtolower($this->getFileExt($tempfilename)),$this->ext))
- {
- returntrue;
- }else{
- returnfalse;
- }
- }
- privatefunctiongetFolder($folder)
- {
- if(!is_dir($folder))
- {
- mkdir($folder);
- }
- return$folder."/";
- }
- publicfunctionupload()
- {
- if($this->uploadtype=="ONE")
- {
- if($this->getExt($this->file["type"]))
- {
- parent::errorExt();
- }elseif($this->file["size"]>self::filesize){
- parent::errorFileSize();
- }elseif(!$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();
- }
- }
- }elseif($this->uploadtype=="ANY"){
- for($i=0;$i<count($this->file);$i++)
- {
- if($this->getExt($this->file[$i]["type"]))
- {
- parent::errorExt();
- }elseif($this->file[$i]["size"]>self::filesize){
- parent::errorFileSize();
- }elseif(!$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();
- }
- }
- publicfunctiongetUploadFileName()
- {
- return$this->uploadfilename;
- }
- publicfunctionsetUploadPath($path)
- {
- $this->uploadpath=$path;
- }
- privatefunctiongetRandomN($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++)
- {
- $randrandn=rand(0,9-$i);
- $return.=$ary_num[$randn];
- $ary_num[$randn]=$ary_num[9-$i];
- }
- return$return;
- }
- publicfunction__destruct()
- {
- $this->uploadfilename=null;
- $this->uploadtype=null;
- $this->file=null;
- $this->savepath=null;
- }
- }
- classError
- {
- publicstaticfunctionerrorFileSize()
- {
- echo"超出最大上傳限制";
- }
- publicstaticfunctionerrorExt()
- {
- echo"此類文件不允許上傳";
- }
- publicstaticfunctionerrorUploadPath()
- {
- echo"上傳路徑不正確";
- }
- publicstaticfunctionerrorMoveUpload()
- {
- echo"上傳失敗";
- }
- publicstaticfunctionokMoved()
- {
- echo"上傳成功!";
- }
- publicstaticfunctionokArrayMoved()
- {
- echo"上傳成功!";
- }