本文标签:PHP上传多个文件
前几天看了一本关于PHP的书,让我感触很深,我先介绍一下PHP的发展史,然后在教大家一个PHP上传多个文件的一个小技巧 。让我们先来简单的介绍一下PHP吧!PHP 最初是1994年Rasmus Lerdorf创建的,刚刚开始只是一个简单的用Perl语言编写的程序,用来统计他自己网站的访问者 。后来又用C语言重新编写,包括可以访问数据库 。
- 关于PHP数组转字符串详细介绍
- 简单快捷PHP数组赋值方法详解
- 关于Apache 2.0和PHP5.0安装详解
- 高手指南PHP安装配置
- 主流Apache 2 PHP5安装使用介绍
以后越来越多的网站使用了PHP,并且强烈要求增加一些特性,比如循环语句和数组变量等等,在新的成员加入开发行列之后,在1995年 中,PHP2.0发布了 。第二版定名为PHP/FI(Form Interpreter) 。PHP/FI加入了对mSQL的支持,从此建立了PHP在动态网页开发上的地位 。到了1996年底,有15000个网站使用 PHP/FI;时间到了1997年中,使用PHP/FI的网站数字超过五万个 。而在1997年中,开始了第三版的开发计划,开发小组加入了 Zeev Suraski 及 Andi Gutmans,而第三版就定名为PHP3 。2000年,PHP4.0又问世了,其中增加了许多新的特性 。以下给大家介绍一个PHP上传多个文件的方法 。
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(); } ?> > <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>UntitledDocumenttitle> 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"上传成功!"; }
|