Five-minute PHP Upload class implementation _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Five-minute PHP Upload class implementation. PHP has many things worth learning. here we will mainly introduce the PHP Upload solution. I hope you will gain a lot through this article. You can directly enter the PHP command on the WEB page on behalf of PHP, which is worth learning. here we will mainly introduce the PHP Upload solution. I hope you will gain a lot through this article. You can directly enter the PHP command code on the WEB page without any special development environment. On the WEB page, all PHP code is placed in. In addition, you can also choose to use the form such. The PHP engine automatically identifies and processes all code on the page between PHP delimiters.

The syntax structure of PHP script is very similar to that of C and Perl. You do not need to declare variables before using them. Using PHP to create an array is also very simple. PHP also has basic object-oriented component functions, which greatly facilitates users to effectively organize and encapsulate their own code. next we will introduce the PHP Upload class in detail.
 
PHP Upload class implementation code:

 
 
  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 file content
  14. * @ Paramstring $ file name generated by fileName
  15. * @ Returnboolean returns true if creation is successful.
  16. * @ Deprecated
  17. * Create an html file
  18. */
  19. FunctioncreateHtml ($ info, $ fileName)
  20. {
  21. }
  22. /**
  23. *
  24. * @ Authorsanshi
  25. * @ Version1.0.0ThuAug1801: 03: 09CST2005
  26. * @ Returnvoid
  27. * @ Deprecated
  28. * Constructor
  29. */
  30. FunctiondownLoad ()
  31. {}
  32. /**
  33. *
  34. * @ Authorsanshi
  35. * @ Version1.0.0ThuAug1801: 03: 55CST2005
  36. * @ Paramstring $ field name of fileField in the form
  37. * @ Paramstring $ length limit
  38. * @ Returnboolean success return true
  39. * @ Deprecated
  40. * Function implementation functions
  41. */
  42. Functioninit ($ fileField, $Length='')
  43. {
  44. $Files= $ _ FILES [$ fileField];
  45. // Change the user name based on your actual situation.
  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']; // This part may not exist in php4
  52. // Change
  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); // Obtain the path
  60. $CreateFileName= $ UserName. "_". time (); // sets the current file name
  61. $CreateFileType= $ This->GetFileType ($ fileName); // sets the file category
  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 indicates the maximum upload size.
  70. * @ Returnboolean returns true if the value exceeds
  71. * @ Deprecated
  72. * Determine whether the specified size is exceeded.
  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 file name
  84. * @ Returnstring $ fileType file suffix
  85. * @ Deprecated
  86. * Get the file suffix (only get the last suffix of the file)
  87. */
  88. FunctiongetFileType ($ fileName)
  89. {
  90. Returnend (explode ('.', $ fileName ));
  91. }
  92. /**
  93. *
  94. * @ Authorsanshi
  95. * @ Version1.0.0ThuAug1801: 10: 41CST2005
  96. * @ Paramstring $ fileName file name
  97. * @ Paramboolean $ method whether to check multiple suffixes; default value: false
  98. * @ Paramint $ the default number of postFix suffixes is 2.
  99. * @ Returnboolean returns true if Yes
  100. * @ Deprecated
  101. * Check whether the file suffix is in the category array. set the category array by yourself.
  102. * If $ method is set to true, check the file with several suffixes.
  103. */
  104. Function_isType ($ fileName, $Method='False', $PostFix=2)
  105. {
  106. // Set the category array
  107. $Type=Array('Jpeg ',
  108. 'Gif ',
  109. 'Bmp ',
  110. 'Exe ');
  111. $FileName=Strtolower($ FileName );
  112. $FileTypeArray=Explode('.', $ FileName );
  113. $FileType=End($ FileTypeArray );
  114. // Determine whether a file has multiple suffixes
  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. * Create a directory in the format of year/month/day/user name/
  132. * The permission is 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 directory name
  161. * @ Return boolean the return value is true.
  162. * @ Deprecated
  163. * Determine whether the operation is in the upload Directory
  164. */
  165. Function_isDel ($ dirName)
  166. {
  167. // Pay attention to upLoadDir, which must correspond to the actual directory used
  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 file name
  178. * @ Returnboolean returns true if the object is successfully deleted.
  179. * @ Deprecated
  180. * Delete an object
  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 directory name
  197. * @ Returnboolean return true if deletion is successful
  198. * @ Deprecated
  199. * Deleting files in the directory cannot be deleted.
  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. // Use
  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. ?>

Bytes. You can directly enter the PHP command generation on the WEB page...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.