PHP upload class upload. php Usage _ PHP Tutorial

Source: Internet
Author: User
PHP upload class upload. php Usage method. What we bring to you today is the specific code as follows :? Php *** my file Upload class ** unfinished functions: * 1. determine whether the target directory exists * 2. what we bring to you today when uploadingThe code is as follows:

 
 
  1. Php
  2. /**
  3. * My file Upload class
  4. *
  5. * Unfinished functions:
  6. * 1. determine whether the target directory exists
  7. * 2. if a duplicate name appears during the upload, it is automatically renamed.
  8. *
  9. * @ Author M.Q.<[Url] www.mengqi.net [/url]>
  10. */
  11. Class upload
  12. {
  13. /**
  14. * PHP upload class upload. php file upload information. this value is obtained by The constructor. if the file upload fails or is not uploaded, this value is false.
  15. *
  16. * @ Var array
  17. */
  18. Private $File=False;
  19. /**
  20. * Constructor: obtains the information of the uploaded file.
  21. *
  22. * If an error occurs in the project where the file is uploaded, the error file will not be returned in the result, and all files in the result are available.
  23. *
  24. * @ Param string $ tag form<Input>Name attribute value in the tag, for example<Input Name="P" Type="File">
  25. *
  26. * Example 1: upload a single file:
  27. *<Input Name="Upfile" Type="File">
  28. *
  29. * Example 2: upload multiple files:
  30. *<Input Name="Upfile []" Type="File">
  31. *<Input Name="Upfile []" Type="File">
  32. *
  33. * The result (saved in the $ file variable) is as follows:
  34. *
  35. * Array (
  36. * [0] =>Array (
  37. * 'Name' =>'Abc.txt'
  38. * 'Type' =>'Text/plain'
  39. * 'Tmp _ name' =>'/Tmp/phpgxecCb'
  40. * 'Error' =>0
  41. * 'Size' =>62
  42. *)
  43. * [1] =>Array (
  44. * 'Name' =>'Abc.txt'
  45. * 'Type' =>'Text/plain'
  46. * 'Tmp _ name' =>'/Tmp/phpgxecCb'
  47. * 'Error' =>0
  48. * 'Size' =>62
  49. *)
  50. *)
  51. */
  52. Public function _ construct ($ tag)
  53. {
  54. $File= $ _ FILES [$ tag];
  55. If (! Isset ($ file) | empty ($ file ))
  56. {
  57. Return; // no file is uploaded
  58. }
  59. $Num=Count($ File ['name']); // number of files uploaded by the PHP upload class upload. php
  60. $Data=Array(); // Array used to save the information of the uploaded file
  61. // Multiple files are uploaded.
  62. If ($ num>1)
  63. {
  64. For ($I=0; $ I<$ Num; $ I ++)
  65. {
  66. $D=Array();
  67. $ D ['name'] = $ file ['name'] [$ I];
  68. $ D ['type'] = $ file ['type'] [$ I];
  69. $ D ['tmp _ name'] = $ file ['tmp _ name'] [$ I];
  70. $ D ['error'] = $ file ['error'] [$ I];
  71. $ D ['size'] = $ file ['size'] [$ I];
  72. If ($ d ['error'] = 0)
  73. {
  74. $ Data [] = $ d;
  75. }
  76. Else
  77. {
  78. @ Unlink ($ d ['tmp _ name']);
  79. }
  80. }
  81. }
  82. // Only one file is uploaded.
  83. Else
  84. {
  85. $D=Array();
  86. $ D ['name'] = $ file ['name'];
  87. $ D ['type'] = $ file ['type'];
  88. $ D ['tmp _ name'] = $ file ['tmp _ name'];
  89. $ D ['error'] = $ file ['error'];
  90. $ D ['size'] = $ file ['size'];
  91. If ($ d ['error'] = 0)
  92. {
  93. $ Data [] = $ d;
  94. }
  95. Else
  96. {
  97. @ Unlink ($ d ['tmp _ name']);
  98. }
  99. }
  100. If (empty ($ data) return;
  101. $ This-> File= $ Data; // save the information of the uploaded file
  102. }
  103. /**
  104. * Move the uploaded file from the temporary folder to the target path.
  105. *
  106. * @ Param array $ src file information array, which is an element of the $ file array (still an array)
  107. * @ Param string $ destpath the destination path for the Upload
  108. * @ Param string $ filename: name of the uploaded file. if it is null, the uploaded file name is used.
  109. * @ Return bool
  110. */
  111. Public function save ($ src, $ destpath, $Filename=Null)
  112. {
  113. $SrcTName= $ Src ['tmp _ name']; // temporary file name of the original uploaded File
  114. $SrcFName= $ Src ['name']; // original file name
  115. // If the $ filename parameter is null, the file name is used during the upload.
  116. If (empty ($ filename ))
  117. {
  118. $Filename= $ SrcFName;
  119. }
  120. // $ Dest is the final path and file name of the file to be copied.
  121. If (empty ($ destpath ))
  122. {
  123. $Dest= $ Filename;
  124. }
  125. Else
  126. {
  127. // Modify the slash in the path to/. if the slash is not at the end, add a slash to the end/
  128. $Pathend= $ Destpath [strlen ($ destpath)-1]; // The last character of the uploaded destination path
  129. If ($Pathend= '\')
  130. {
  131. $Dest=Substr_replace($ Destpath, '/', strlen ($ destpath)-1). $ filename;
  132. }
  133. Else if ($ pathend! = '/')
  134. {
  135. $Dest= $ Destpath. '/'. $ filename;
  136. }
  137. Else
  138. {
  139. $Dest= $ Destpath. $ filename;
  140. }
  141. }
  142. // File uploaded successfully
  143. If (@ move_uploaded_file ($ srcTName, $ dest ))
  144. {
  145. Return true;
  146. }
  147. Else
  148. {
  149. Return false;
  150. }
  151. }
  152. /**
  153. * Obtain the information of the uploaded file
  154. *
  155. * @ Return array
  156. */
  157. Public function getFileInfo ()
  158. {
  159. Return $ this->File;
  160. }
  161. }
  162. $A=NewUpload ('upfile ');
  163. $Fileinfo= $->GetFileInfo ();
  164. If ($Fileinfo= False)
  165. {
  166. Echo 'no file uploaded! ';
  167. Exit;
  168. }
  169. For ($I=0; $ I< Count($ Fileinfo); $ I ++)
  170. {
  171. Echo 'uploading '. $ fileinfo [$ I] ['name']. '';
  172. If ($->Save ($ fileinfo [$ I], 'upload') echo 'complete ';
  173. Else echo 'failed ';
  174. Echo'<Br>';
  175. }
  176. ?>

The above code describes how to use PHP upload class upload. php.


The specific code of callback is as follows :? Php/*** my file Upload class ** unfinished features: * 1. determine whether the target directory exists * 2. if the file is uploaded...

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.