五分钟PHP上传类实现

开发 后端
这里介绍了PHP上传类的案例分析,文章有详细的代码,复制粘贴一下就可以跑在机器上使用,希望对大家有帮助。

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

#T#PHP脚本语言的语法结构与C语言和Perl语言的语法风格非常相似。用户在使用变量前不需要对变量进行声明。使用PHP创建数组的过程也非常简单。PHP还具有基本的面向对象组件功能,可以极大的方便用户有效组织和封装自己编写的代码,下面我们就详细的介绍PHP上传类的问题。
 
PHP上传类实现代码:

  1. <?php 
  2. /**  
  3. *Fileuploadclass  
  4. *@version1.0.0(ThuAug1801:32:39CST2005)  
  5. *@authorsanshi  
  6. */  
  7. classupLoad  
  8. {  
  9. /**  
  10. *  
  11. *@authorsanshi  
  12. *@version1.0.0ThuAug1801:00:18CST2005  
  13. *@paramstring$info文件内容  
  14. *@paramstring$fileName生成的文件名  
  15. *@returnboolean建立成功返回true  
  16. *@deprecated  
  17. *建立html文件  
  18. */  
  19. functioncreateHtml($info,$fileName)  
  20. {  
  21. }  
  22. /**  
  23. *  
  24. *@authorsanshi  
  25. *@version1.0.0ThuAug1801:03:09CST2005  
  26. *@returnvoid  
  27. *@deprecated  
  28. *构造函数  
  29. */  
  30. functiondownLoad()  
  31. {}  
  32. /**  
  33. *  
  34. *@authorsanshi  
  35. *@version1.0.0ThuAug1801:03:55CST2005  
  36. *@paramstring$fileField在表单中的字段名  
  37. *@paramstring$length限制的长度  
  38. *@returnboolean成功返回true  
  39. *@deprecated  
  40. *功能实现函数  
  41. */  
  42. functioninit($fileField,$length='')  
  43. {  
  44. $files=$_FILES[$fileField];  
  45. //用户名需要改动,根据自己的实际情况做改动  
  46. $userName='sanshi';  
  47. $fileName=$files['name'];  
  48. $fileType=$files['type'];  
  49. $fileTemp=$files['tmp_name'];  
  50. $fileSize=empty($length)?($files['size']+10):$length;  
  51. $fileError=$files['error'];//这块也许php4中没有  
  52. //改为  
  53. //if($this->_isType($fileName)||$this->_isBig($length ))  
  54. if(!$this->_isType($fileName)||$this->_isBig($length )||$fileError!=0)  
  55. {  
  56. //print_r($files);  
  57. returnfalse;  
  58. }else{  
  59. $path=$this->_createDir($userName);//取得路径  
  60. $createFileName=$userName."_".time();//设置当前文件名  
  61. $createFileType=$this->getFileType($fileName);//设置文件类别  
  62. return@move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType)?true:false;  
  63. }  
  64. }  
  65.  
  66. /**  
  67. *  
  68. *@authorsanshi  
  69. *@version1.0.0ThuAug1801:07:43CST2005  
  70. *@paramint$length上传限制的大小  
  71. *@returnboolean超过返回true  
  72. *@deprecated  
  73. *判断是否超过预定大小  
  74. */  
  75. function_isBig($length)  
  76. {  
  77. $bigest='';  
  78. return$big>$bigest?true:false;  
  79. }  
  80. /**  
  81. *  
  82. *@authorsanshi  
  83. *@version1.0.0ThuAug1801:08:55CST2005  
  84. *@paramstring$fileName文件名  
  85. *@returnstring$fileType文件后缀  
  86. *@deprecated  
  87. *取得文件后缀(只取得文件的***一个后缀名)  
  88. */  
  89. functiongetFileType($fileName)  
  90. {  
  91. returnend(explode('.',$fileName));  
  92. }  
  93. /**  
  94. *  
  95. *@authorsanshi  
  96. *@version1.0.0ThuAug1801:10:41CST2005  
  97. *@paramstring$fileName文件名  
  98. *@paramboolean$method是否检查多个后缀默认false  
  99. *@paramint$postFix后缀个数默认为2  
  100. *@returnboolean存在返回true  
  101. *@deprecated  
  102. *检查文件的后缀是否在类别数组中,类别数组自己设置  
  103. *如果$method设置为true则检查文件有几个后缀  
  104. */  
  105. function_isType($fileName,$method='false',$postFix=2)  
  106. {  
  107. //设置类别数组  
  108. $type=array('jpeg',  
  109. 'gif',  
  110. 'bmp',  
  111. 'exe');  
  112. $fileName=strtolower($fileName);  
  113. $fileTypeArray=explode('.',$fileName);  
  114. $fileType=end($fileTypeArray);  
  115. //判断是否有一个文件有多个后缀  
  116. if($method)  
  117. {  
  118. if(count($fileTypeArray)>(is_int($postFix)?$postFix:2))  
  119. {  
  120. returnfalse;  
  121. }  
  122. }  
  123. returnin_array($fileType,$type);  
  124. }  
  125.  
  126. /**  
  127. *  
  128. *@authorsanshi  
  129. *@version1.0.0ThuAug1801:17:19CST2005  
  130. *@paramstring$userName  
  131. *@returnstring$path  
  132. *@deprecated  
  133. *建立目录目录格式年/月/日/用户名/  
  134. *权限为755  
  135. */  
  136. function_createDir($userName)  
  137. {  
  138. $root='';  
  139. $pathSign=DIRECTORY_SEPARATOR;  
  140. $y=date('Y').$pathSign;  
  141. $m=date('m').$pathSign;  
  142. $d=date('d').$pathSign;  
  143. $path=$root.$y.$m.$d.$userName;  
  144. $dirArray=explode($pathSign,$path);  
  145. $tempDir='';  
  146. foreach($dirArrayas$dir)  
  147. {  
  148. $tempDir.=$dir.$pathSign;  
  149. $isFile=file_exists($tempDir);  
  150. clearstatcache();  
  151. if(!$isFile&&!is_dir($tempDir))  
  152. {  
  153. @mkdir($tempDir,0755);  
  154. }  
  155. }  
  156. return$path.$pathSign;  
  157. }  
  158. /**  
  159. *  
  160. *@authorsanshi  
  161. *@version1.0.0ThuAug1801:19:32CST2005  
  162. *@param string$dirName目录名  
  163. *@return boolean可以操作返回true  
  164. *@deprecated  
  165. *判断操作是否在上传目录  
  166. */  
  167. function_isDel($dirName)  
  168. {  
  169. //注意upLoadDir,一定要与真正使用目录相对应  
  170. $upLoadDir='';  
  171. $upLoadDir=preg_replace('/\\//','\/',$upLoadDir);  
  172. $format="/^{$upLoadDir}/";  
  173. returnpreg_match($format,$dirName);  
  174. }  
  175. /**  
  176. *  
  177. *@authorsanshi  
  178. *@version1.0.0ThuAug1801:25:58CST2005  
  179. *@paramstring$fileName文件名  
  180. *@returnboolean删除文件成功返回true  
  181. *@deprecated  
  182. *删除文件  
  183. */  
  184. functiondelFile($fileName)  
  185. {  
  186. $cur_dir=dirname(trim($fileName));  
  187. if($this->_isDel($cur_dir))  
  188. {  
  189. return@unlink($fileName)?true:false;  
  190. }else{  
  191. returnfalse;  
  192. }  
  193. }  
  194. /**  
  195. *  
  196. *@authorsanshi  
  197. *@version1.0.0ThuAug1801:27:43CST2005  
  198. *@paramstring$dieName目录名  
  199. *@returnboolean删除成功返回true  
  200. *@deprecated  
  201. *删除目录目录下如果有文件不能删除  
  202. */  
  203. functiondelDir($dirName)  
  204. {  
  205. if($this->_isDel($dirName)&&is_dir($dirName))  
  206. {  
  207. return@rmdir($dirName)?true:false;  
  208. }else{  
  209. returnfalse;  
  210. }  
  211. }  
  212.  
  213. }  
  214. ?> 
  215. <?php 
  216. //使用  
  217. /*  
  218. include'upLoad.class.php';  
  219. $up=newupLoad();  
  220. if($up->init("file"))  
  221. {  
  222. echo'success';  
  223. }else{  
  224. echo'failure';  
  225. }  
  226. */  
  227. ?> 
责任编辑:田树 来源: 博客
相关推荐

2009-11-16 11:30:55

PHP上传文件代码

2009-10-26 15:45:43

VB.NET类构造

2011-08-08 09:22:10

Python

2009-11-17 12:47:05

PHP配置

2009-10-21 18:19:36

VB.NET实现拖放

2009-10-30 13:12:40

VB.NET创建类

2009-11-16 10:53:30

Oracle Hint

2019-08-09 10:33:36

开发技能代码

2021-06-07 09:51:22

原型模式序列化

2009-11-05 14:53:54

Visual Stud

2009-10-22 16:18:19

Oracle表空间

2021-10-19 07:27:08

HTTP代理网络

2020-06-16 08:47:53

磁盘

2021-03-03 16:33:24

web项目外网

2022-12-13 10:05:27

定时任务任务调度操作系统

2023-07-16 18:49:42

HTTP网络

2020-11-10 09:01:52

DPDK网络监控

2023-07-26 07:11:50

LVM底层抽象

2021-06-06 13:08:22

C#特性Attribute

2017-12-20 09:42:39

PythonNginx日志
点赞
收藏

51CTO技术栈公众号