五分鐘PHP上傳類實現_PHP教程

來源:互聯網
上載者:User
PHP有很多值得學習的地方,這裡我們主要介紹PHP上傳類的解決方案,希望大家通過本文有大的收穫。使用者可以直接在WEB頁面中輸入PHP命令代碼,因而不需要任何特殊的開發環境。在WEB頁面中,所有PHP代碼都被放置在“”中。此外,使用者還可以選擇使用諸如 等的形式。PHP引擎會自動識別並處理頁面中所有位於PHP定界符之間的代碼。

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. *@authorsanshi
  68. *@version1.0.0ThuAug1801:07:43CST2005
  69. *@paramint$length上傳限制的大小
  70. *@returnboolean超過返回true
  71. *@deprecated
  72. *判斷是否超過預定大小
  73. */
  74. function_isBig($length)
  75. {
  76. $bigest='';
  77. return$big>$bigest?true:false;
  78. }
  79. /**
  80. *
  81. *@authorsanshi
  82. *@version1.0.0ThuAug1801:08:55CST2005
  83. *@paramstring$fileName檔案名稱
  84. *@returnstring$fileType檔案尾碼
  85. *@deprecated
  86. *取得檔案尾碼(只取得檔案的最後一個尾碼名)
  87. */
  88. functiongetFileType($fileName)
  89. {
  90. returnend(explode('.',$fileName));
  91. }
  92. /**
  93. *
  94. *@authorsanshi
  95. *@version1.0.0ThuAug1801:10:41CST2005
  96. *@paramstring$fileName檔案名稱
  97. *@paramboolean$method是否檢查多個尾碼預設false
  98. *@paramint$postFix尾碼個數預設為2
  99. *@returnboolean存在返回true
  100. *@deprecated
  101. *檢查檔案的尾碼是否在類別數組中,類別數組自己設定
  102. *如果$method設定為true則檢查檔案有幾個尾碼
  103. */
  104. function_isType($fileName,$method='false',$postFix=2)
  105. {
  106. //設定類別數組
  107. $type=array('jpeg',
  108. 'gif',
  109. 'bmp',
  110. 'exe');
  111. $fileName=strtolower($fileName);
  112. $fileTypeArray=explode('.',$fileName);
  113. $fileType=end($fileTypeArray);
  114. //判斷是否有一個檔案有多個尾碼
  115. if($method)
  116. {
  117. if(count($fileTypeArray)>(is_int($postFix)?$postFix:2))
  118. {
  119. returnfalse;
  120. }
  121. }
  122. returnin_array($fileType,$type);
  123. }
  124. /**
  125. *
  126. *@authorsanshi
  127. *@version1.0.0ThuAug1801:17:19CST2005
  128. *@paramstring$userName
  129. *@returnstring$path
  130. *@deprecated
  131. *建立目錄目錄格式年/月/日/使用者名稱/
  132. *許可權為755
  133. */
  134. function_createDir($userName)
  135. {
  136. $root='';
  137. $pathSign=DIRECTORY_SEPARATOR;
  138. $y=date('Y').$pathSign;
  139. $m=date('m').$pathSign;
  140. $d=date('d').$pathSign;
  141. $path=$root.$y.$m.$d.$userName;
  142. $dirArray=explode($pathSign,$path);
  143. $tempDir='';
  144. foreach($dirArrayas$dir)
  145. {
  146. $tempDir.=$dir.$pathSign;
  147. $isFile=file_exists($tempDir);
  148. clearstatcache();
  149. if(!$isFile&&!is_dir($tempDir))
  150. {
  151. @mkdir($tempDir,0755);
  152. }
  153. }
  154. return$path.$pathSign;
  155. }
  156. /**
  157. *
  158. *@authorsanshi
  159. *@version1.0.0ThuAug1801:19:32CST2005
  160. *@param string$dirName目錄名
  161. *@return boolean可以操作返回true
  162. *@deprecated
  163. *判斷操作是否在上傳目錄
  164. */
  165. function_isDel($dirName)
  166. {
  167. //注意upLoadDir,一定要與真正使用目錄相對應
  168. $upLoadDir='';
  169. $upLoadDir=preg_replace('/\//','/',$upLoadDir);
  170. $format="/^{$upLoadDir}/";
  171. returnpreg_match($format,$dirName);
  172. }
  173. /**
  174. *
  175. *@authorsanshi
  176. *@version1.0.0ThuAug1801:25:58CST2005
  177. *@paramstring$fileName檔案名稱
  178. *@returnboolean刪除檔案成功返回true
  179. *@deprecated
  180. *刪除檔案
  181. */
  182. functiondelFile($fileName)
  183. {
  184. $cur_dir=dirname(trim($fileName));
  185. if($this->_isDel($cur_dir))
  186. {
  187. return@unlink($fileName)?true:false;
  188. }else{
  189. returnfalse;
  190. }
  191. }
  192. /**
  193. *
  194. *@authorsanshi
  195. *@version1.0.0ThuAug1801:27:43CST2005
  196. *@paramstring$dieName目錄名
  197. *@returnboolean刪除成功返回true
  198. *@deprecated
  199. *刪除目錄目錄下如果有檔案不能刪除
  200. */
  201. functiondelDir($dirName)
  202. {
  203. if($this->_isDel($dirName)&&is_dir($dirName))
  204. {
  205. return@rmdir($dirName)?true:false;
  206. }else{
  207. returnfalse;
  208. }
  209. }
  210. }
  211. ?>
  212. php
  213. //使用
  214. /*
  215. include'upLoad.class.php';
  216. $up=newupLoad();
  217. if($up->init("file"))
  218. {
  219. echo'success';
  220. }else{
  221. echo'failure';
  222. }
  223. */
  224. ?>

http://www.bkjia.com/PHPjc/446561.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446561.htmlTechArticlePHP有很多值得學習的地方,這裡我們主要介紹PHP上傳類的解決方案,希望大家通過本文有大的收穫。使用者可以直接在WEB頁面中輸入PHP命令代...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.