PHP Upload class upload.php How to use _php tutorial

Source: Internet
Author: User
What we are bringing to you today is aboutThe specific code is as follows:

 
 
  1. php
  2. /**
  3. * My file Upload class
  4. *
  5. * Unfinished Features:
  6. * 1. The determination of the existence of the target directory
  7. * 2. Automatically rename if duplicate name appears on upload
  8. *
  9. * @author m.q. < [Url]www.mengqi.net[/url] >
  10. */
  11. Class upload
  12. {
  13. /**
  14. * PHP upload class upload.php Upload the information of the file, this value is obtained by the constructor, if the upload file failed or error or not uploaded, this value is False
  15. *
  16. * @var Array
  17. */
  18. Private $ file = false ;
  19. /**
  20. * Constructor: Get the information to upload the file
  21. *
  22. * If there is an error in the project of uploading the file, then the error file will not be returned in the result, the files in the result will be available.
  23. *
  24. * @param string $tag form form <input> the value of the Name property in the label, example <input name="P" type="File" >
  25. *
  26. * Example 1, upload a single file:
  27. * <input name="upfile" type="file" >
  28. *
  29. * Example 2, uploading multiple files:
  30. * <input name="upfile[]" type="File" >
  31. * <input name="upfile[]" type="file" >
  32. *
  33. * Results (stored in the $file variable) are 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 ' = > +
  42. * )
  43. * [1] = > Array (
  44. * ' name ' = > ' Abc.txt '
  45. * ' type ' = > ' Text/plain '
  46. * ' tmp_name ' = > '/TMP/PHPGXECCB '
  47. * ' ERROR ' = > 0
  48. * ' size ' = > +
  49. * )
  50. * )
  51. */
  52. Public function __construct ($tag)
  53. {
  54. $ file = $_files[$tag];
  55. if (!isset ($file) | | | empty ($file))
  56. {
  57. Return No files Uploaded
  58. }
  59. $ Num = Count ($file [' name ']);//php upload class upload.php number of uploaded files
  60. $ Data = Array ();//An array of information used to save the uploaded file
  61. Uploaded multiple Files
  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 was 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 information for uploading files
  102. }
  103. /**
  104. * Move uploaded files from the Temp folder to the destination path
  105. *
  106. * @param array of $src file information, is one of the elements of the $file array (still an array)
  107. * @param string $destpath The target path of the upload
  108. * @param string $filename The file name after uploading, and if it is empty, use the file name when uploading
  109. * @return BOOL
  110. */
  111. Public function Save ($SRC, $destpath, $filename = null)
  112. {
  113. $ Srctname = $src [' tmp_name '];//temporary file name of the original upload
  114. $ Srcfname = $src [' name ']; Original file name
  115. If the $filename parameter is empty, the file name when uploading is used
  116. if (empty ($filename))
  117. {
  118. $ filename = $srcFName;
  119. }
  120. $dest is the path and file name to which the files will eventually be copied
  121. if (empty ($destpath))
  122. {
  123. $ dest = $filename;
  124. }
  125. Else
  126. {
  127. Fix the slash in the path, change the end to/, and if not at the end, add a/
  128. $ Pathend = $destpath [Strlen ($destpath)-1];//The last character of the destination path to upload
  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. Upload file successfully
  143. if (@move_uploaded_file ($srcTName, $dest))
  144. {
  145. return true;
  146. }
  147. Else
  148. {
  149. return false;
  150. }
  151. }
  152. /**
  153. * Get information on uploading files
  154. *
  155. * @return Array
  156. */
  157. Public Function GetFileInfo ()
  158. {
  159. return $this- > file;
  160. }
  161. }
  162. $ a = New upload (' upfile ');
  163. $ FileInfo = $a- > GetFileInfo ();
  164. if ($fileinfo = = False)
  165. {
  166. Echo ' didn't upload the file! ';
  167. Exit
  168. }
  169. For ($i = 0; $i < Count($ FileInfo); $i + +)
  170. {
  171. Echo ' uploading '. $fileinfo [$i] [' name ']. ' ';
  172. if ($a-> Save ($fileinfo [$i], ' upload ')) echo ' complete ';
  173. else echo ' failure ';
  174. Echo ' <br> ';
  175. }
  176. ?>

The above code is about PHP upload class upload.php The specific use of the method.


http://www.bkjia.com/PHPjc/446389.html www.bkjia.com true http://www.bkjia.com/PHPjc/446389.html techarticle What we are bringing to you today is about the specific code as follows: PHP/** * My file Upload class * * Unfinished function: * *. If the target directory is present .

  • 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.