五分钟PHP上传类实现_PHP教程

WBOY
Freigeben: 2016-07-15 13:26:53
Original
799 Leute haben es durchsucht

PHP有很多值得学习的地方,这里我们主要介绍PHP上传类的解决方案,希望大家通过本文有大的收获。用户可以直接在WEB页面中输入PHP命令代码,因而不需要任何特殊的开发环境。在WEB页面中,所有PHP代码都被放置在“”中。此外,用户还可以选择使用诸如 等的形式。PHP引擎会自动识别并处理页面中所有位于PHP定界符之间的代码。

PHP脚本语言的语法结构与C语言和Perl语言的语法风格非常相似。用户在使用变量前不需要对变量进行声明。使用PHP创建数组的过程也非常简单。PHP还具有基本的面向对象组件功能,可以极大的方便用户有效组织和封装自己编写的代码,下面我们就详细的介绍PHP上传类的问题。
 
PHP上传类实现代码:
<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*Fileuploadclass  </span></li>
<li class=""><span>*@version1.0.0(ThuAug1801:32:39CST2005)  </span></li>
<li class="alt"><span>*@authorsanshi  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>classupLoad  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>/**  </span></li>
<li class=""><span>*  </span></li>
<li class="alt"><span>*@authorsanshi  </span></li>
<li class=""><span>*@version1.0.0ThuAug1801:00:18CST2005  </span></li>
<li class="alt"><span>*@paramstring$info文件内容  </span></li>
<li class=""><span>*@paramstring$fileName生成的文件名  </span></li>
<li class="alt"><span>*@returnboolean建立成功返回true  </span></li>
<li class=""><span>*@deprecated  </span></li>
<li class="alt"><span>*建立html文件  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>functioncreateHtml($info,$fileName)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*  </span></li>
<li class=""><span>*@authorsanshi  </span></li>
<li class="alt"><span>*@version1.0.0ThuAug1801:03:09CST2005  </span></li>
<li class=""><span>*@returnvoid  </span></li>
<li class="alt"><span>*@deprecated  </span></li>
<li class=""><span>*构造函数  </span></li>
<li class="alt"><span>*/  </span></li>
<li class=""><span>functiondownLoad()  </span></li>
<li class="alt"><span>{}  </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*  </span></li>
<li class=""><span>*@authorsanshi  </span></li>
<li class="alt"><span>*@version1.0.0ThuAug1801:03:55CST2005  </span></li>
<li class=""><span>*@paramstring$fileField在表单中的字段名  </span></li>
<li class="alt"><span>*@paramstring$length限制的长度  </span></li>
<li class=""><span>*@returnboolean成功返回true  </span></li>
<li class="alt"><span>*@deprecated  </span></li>
<li class=""><span>*功能实现函数  </span></li>
<li class="alt"><span>*/  </span></li>
<li class="">
<span>functioninit($fileField,$</span><span class="attribute">length</span><span>=</span><span class="attribute-value">''</span><span>)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute">files</span><span>=$_FILES[$fileField];  </span>
</li>
<li class="alt"><span>//用户名需要改动,根据自己的实际情况做改动  </span></li>
<li class="">
<span>$</span><span class="attribute">userName</span><span>=</span><span class="attribute-value">'sanshi'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">fileName</span><span>=$files['name'];  </span>
</li>
<li class="">
<span>$</span><span class="attribute">fileType</span><span>=$files['type'];  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">fileTemp</span><span>=$files['tmp_name'];  </span>
</li>
<li class="">
<span>$</span><span class="attribute">fileSize</span><span>=</span><span class="attribute-value">empty</span><span>($length)?($files['size']+10):$length;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">fileError</span><span>=$files['error'];//这块也许php4中没有  </span>
</li>
<li class=""><span>//改为  </span></li>
<li class="alt">
<span>//if($this-</span><span class="tag">></span><span>_isType($fileName)||$this-</span><span class="tag">></span><span>_isBig($length ))  </span>
</li>
<li class="">
<span>if(!$this-</span><span class="tag">></span><span>_isType($fileName)||$this-</span><span class="tag">></span><span>_isBig($length )||$fileError!=0)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>//print_r($files);  </span></li>
<li class="alt"><span>returnfalse;  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">path</span><span>=$this-</span><span class="tag">></span><span>_createDir($userName);//取得路径  </span>
</li>
<li class="">
<span>$</span><span class="attribute">createFileName</span><span>=$userName."_".time();//设置当前文件名  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">createFileType</span><span>=$this-</span><span class="tag">></span><span>getFileType($fileName);//设置文件类别  </span>
</li>
<li class=""><span>return@move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType)?true:false;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*  </span></li>
<li class=""><span>*@authorsanshi  </span></li>
<li class="alt"><span>*@version1.0.0ThuAug1801:07:43CST2005  </span></li>
<li class=""><span>*@paramint$length上传限制的大小  </span></li>
<li class="alt"><span>*@returnboolean超过返回true  </span></li>
<li class=""><span>*@deprecated  </span></li>
<li class="alt"><span>*判断是否超过预定大小  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>function_isBig($length)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">bigest</span><span>=</span><span class="attribute-value">''</span><span>;  </span>
</li>
<li class="">
<span>return$big</span><span class="tag">></span><span>$bigest?true:false;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*  </span></li>
<li class=""><span>*@authorsanshi  </span></li>
<li class="alt"><span>*@version1.0.0ThuAug1801:08:55CST2005  </span></li>
<li class=""><span>*@paramstring$fileName文件名  </span></li>
<li class="alt"><span>*@returnstring$fileType文件后缀  </span></li>
<li class=""><span>*@deprecated  </span></li>
<li class="alt"><span>*取得文件后缀(只取得文件的最后一个后缀名)  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>functiongetFileType($fileName)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>returnend(explode('.',$fileName));  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>/**  </span></li>
<li class=""><span>*  </span></li>
<li class="alt"><span>*@authorsanshi  </span></li>
<li class=""><span>*@version1.0.0ThuAug1801:10:41CST2005  </span></li>
<li class="alt"><span>*@paramstring$fileName文件名  </span></li>
<li class=""><span>*@paramboolean$method是否检查多个后缀默认false  </span></li>
<li class="alt"><span>*@paramint$postFix后缀个数默认为2  </span></li>
<li class=""><span>*@returnboolean存在返回true  </span></li>
<li class="alt"><span>*@deprecated  </span></li>
<li class=""><span>*检查文件的后缀是否在类别数组中,类别数组自己设置  </span></li>
<li class="alt"><span>*如果$method设置为true则检查文件有几个后缀  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt">
<span>function_isType($fileName,$</span><span class="attribute">method</span><span>=</span><span class="attribute-value">'false'</span><span>,$</span><span class="attribute">postFix</span><span>=</span><span class="attribute-value">2</span><span>)  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt"><span>//设置类别数组  </span></li>
<li class="">
<span>$</span><span class="attribute">type</span><span>=</span><span class="attribute-value">array</span><span>('jpeg',  </span>
</li>
<li class="alt"><span>'gif',  </span></li>
<li class=""><span>'bmp',  </span></li>
<li class="alt"><span>'exe');  </span></li>
<li class="">
<span>$</span><span class="attribute">fileName</span><span>=</span><span class="attribute-value">strtolower</span><span>($fileName);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">fileTypeArray</span><span>=</span><span class="attribute-value">explode</span><span>('.',$fileName);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">fileType</span><span>=</span><span class="attribute-value">end</span><span>($fileTypeArray);  </span>
</li>
<li class="alt"><span>//判断是否有一个文件有多个后缀  </span></li>
<li class=""><span>if($method)  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>if(count($fileTypeArray)</span><span class="tag">></span><span>(is_int($postFix)?$postFix:2))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>returnfalse;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>returnin_array($fileType,$type);  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*  </span></li>
<li class=""><span>*@authorsanshi  </span></li>
<li class="alt"><span>*@version1.0.0ThuAug1801:17:19CST2005  </span></li>
<li class=""><span>*@paramstring$userName  </span></li>
<li class="alt"><span>*@returnstring$path  </span></li>
<li class=""><span>*@deprecated  </span></li>
<li class="alt"><span>*建立目录目录格式年/月/日/用户名/  </span></li>
<li class=""><span>*权限为755  </span></li>
<li class="alt"><span>*/  </span></li>
<li class=""><span>function_createDir($userName)  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute">root</span><span>=</span><span class="attribute-value">''</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">pathSign</span><span>=</span><span class="attribute-value">DIRECTORY_SEPARATOR</span><span>;  </span>
</li>
<li class="">
<span>$</span><span class="attribute">y</span><span>=</span><span class="attribute-value">date</span><span>('Y').$pathSign;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">m</span><span>=</span><span class="attribute-value">date</span><span>('m').$pathSign;  </span>
</li>
<li class="">
<span>$</span><span class="attribute">d</span><span>=</span><span class="attribute-value">date</span><span>('d').$pathSign;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">path</span><span>=$root.$y.$m.$d.$userName;  </span>
</li>
<li class="">
<span>$</span><span class="attribute">dirArray</span><span>=</span><span class="attribute-value">explode</span><span>($pathSign,$path);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">tempDir</span><span>=</span><span class="attribute-value">''</span><span>;  </span>
</li>
<li class=""><span>foreach($dirArrayas$dir)  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute">tempDir.</span><span>=$dir.$pathSign;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">isFile</span><span>=</span><span class="attribute-value">file_exists</span><span>($tempDir);  </span>
</li>
<li class=""><span>clearstatcache();  </span></li>
<li class="alt"><span>if(!$isFile&&!is_dir($tempDir))  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>@mkdir($tempDir,0755);  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>return$path.$pathSign;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*  </span></li>
<li class=""><span>*@authorsanshi  </span></li>
<li class="alt"><span>*@version1.0.0ThuAug1801:19:32CST2005  </span></li>
<li class=""><span>*@param string$dirName目录名  </span></li>
<li class="alt"><span>*@return boolean可以操作返回true  </span></li>
<li class=""><span>*@deprecated  </span></li>
<li class="alt"><span>*判断操作是否在上传目录  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>function_isDel($dirName)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>//注意upLoadDir,一定要与真正使用目录相对应  </span></li>
<li class="">
<span>$</span><span class="attribute">upLoadDir</span><span>=</span><span class="attribute-value">''</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">upLoadDir</span><span>=</span><span class="attribute-value">preg_replace</span><span>('/\//','/',$upLoadDir);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">format</span><span>=</span><span class="attribute-value">"/^{$upLoadDir}/"</span><span>;  </span>
</li>
<li class="alt"><span>returnpreg_match($format,$dirName);  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>/**  </span></li>
<li class=""><span>*  </span></li>
<li class="alt"><span>*@authorsanshi  </span></li>
<li class=""><span>*@version1.0.0ThuAug1801:25:58CST2005  </span></li>
<li class="alt"><span>*@paramstring$fileName文件名  </span></li>
<li class=""><span>*@returnboolean删除文件成功返回true  </span></li>
<li class="alt"><span>*@deprecated  </span></li>
<li class=""><span>*删除文件  </span></li>
<li class="alt"><span>*/  </span></li>
<li class=""><span>functiondelFile($fileName)  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute">cur_dir</span><span>=</span><span class="attribute-value">dirname</span><span>(trim($fileName));  </span>
</li>
<li class="alt">
<span>if($this-</span><span class="tag">></span><span>_isDel($cur_dir))  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return@unlink($fileName)?true:false;  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>returnfalse;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>/**  </span></li>
<li class="alt"><span>*  </span></li>
<li class=""><span>*@authorsanshi  </span></li>
<li class="alt"><span>*@version1.0.0ThuAug1801:27:43CST2005  </span></li>
<li class=""><span>*@paramstring$dieName目录名  </span></li>
<li class="alt"><span>*@returnboolean删除成功返回true  </span></li>
<li class=""><span>*@deprecated  </span></li>
<li class="alt"><span>*删除目录目录下如果有文件不能删除  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt"><span>functiondelDir($dirName)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>if($this-</span><span class="tag">></span><span>_isDel($dirName)&&is_dir($dirName))  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return@rmdir($dirName)?true:false;  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>returnfalse;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="tag">?></span><span> </span>
</li>
<li class="alt">
<span></span><span class="tag"></span><span class="tag-name">php</span><span> </span>
</li>
<li class=""><span>//使用  </span></li>
<li class="alt"><span>/*  </span></li>
<li class=""><span>include'upLoad.class.php';  </span></li>
<li class="alt">
<span>$</span><span class="attribute">up</span><span>=</span><span class="attribute-value">newupLoad</span><span>();  </span>
</li>
<li class="">
<span>if($up-</span><span class="tag">></span><span>init("file"))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>echo'success';  </span></li>
<li class="alt"><span>}else{  </span></li>
<li class=""><span>echo'failure';  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>*/  </span></li>
<li class="alt">
<span></span><span class="tag">?></span><span> </span>
</li>
</ol>
Nach dem Login kopieren

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446561.htmlTechArticlePHP有很多值得学习的地方,这里我们主要介绍PHP上传类的解决方案,希望大家通过本文有大的收获。用户可以直接在WEB页面中输入PHP命令代...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage