Analysis of PHP Generic file Upload classes-PHP Tutorial

Source: Internet
Author: User
The specific parsing of PHP Generic file Upload class. For example, if you can upload an exhibit and upload a thumbnail for it, the restricted type of the thumbnail file may be jpg, gif, and png, the following is a detailed explanation.For example, if you can upload an exhibit and upload a thumbnail for it, the restricted type of the thumbnail file may be jpg, gif, and png, the maximum types of exhibits may be mov, avi, mpeg, and so on, while the image size may be limited to kB, and the audio video size may be limited to 2 MB. The class code is as follows:

 
 
  1. Class Upload
  2. {
  3. Public $ InputName; // control name of the file upload domain
  4. /**
  5. * File types that can be uploaded
  6. * The format is array ('image/jpeg ', 'image/png', 'image/GIF') or an array containing this array (corresponding to each Upload field control)
  7. */
  8. Public $ FileType;
  9. /**
  10. * Maximum size of uploaded files (unit: byte)
  11. * Format: array ('image' =>$ Size, 'Audio' =>$ Size) (indicates the upload size corresponding to each application file type) or an array containing this array (corresponding to each Upload field control) or a value (indicating that all uploaded files are limited to this size)
  12. */
  13. Public $ FileMaxSize;
  14. Public $ FileSavePath; // file storage path (which can be an array, indicating that files are uploaded to different paths in different Upload domains)
  15. Public $ FileSaveName; // file storage name (excluding the extension name) (which can be an array, indicating different names of uploaded files stored in different Upload domains)
  16. Public $ NoteFileFalse; // file error prompt
  17. Public $ NoteFileType; // file type mismatch prompt
  18. Public $ NoteFileSize; // the error code returned when the file size exceeds the specified value.
  19. /* Upload the file and return the file name information (including the suffix )*/
  20. Public function UploadFile ()
  21. {
  22. $ This->CheckFile (); // check the file
  23. $File= $ _ FILES [$ this->InputName];
  24. $File_save_full_name=Array(); // File storage name (including suffix)
  25. Foreach ($ file ['name'] as $Key=>$ Name)
  26. {
  27. If (! Empty ($ name) // The file is not empty.
  28. {
  29. /* Determine the file storage path */
  30. If (is_array ($ this->FileSavePath ))
  31. {
  32. $File_save_path= $ This->FileSavePath [$ key];
  33. }
  34. Else
  35. {
  36. $File_save_path= $ This->FileSavePath;
  37. }
  38. /* Determine the file storage name (excluding the extension name )*/
  39. If (is_array ($ this->FileSaveName ))
  40. {
  41. $File_save_name= $ This->FileSaveName [$ key];
  42. }
  43. Else
  44. {
  45. $File_save_name= $ This->FileSaveName;
  46. }
  47. /* Start to save */
  48. $ This->CreatePath ($ file_save_path); // create a path if the path does not exist
  49. Move_uploaded_file ($ file ["tmp_name"] [$ key], $ file_save_path. $ file_save_name. $ this->GetSuffix ($ file ['name'] [$ key]);
  50. $ File_save_full_name [] = $ file_save_name. $ this->GetSuffix ($ file ['name'] [$ key]);
  51. }
  52. Else
  53. {
  54. $ File_save_full_name [] = null;
  55. }
  56. }
  57. Unlink ($ file );
  58. /* If there is only one file, a single file name is returned */
  59. If (count ($ file_save_full_name) = 1)
  60. {
  61. $File_save_full_name= $ File_save_full_name [0];
  62. }
  63. Return $ file_save_full_name;
  64. }
  65. /* Inspection File */
  66. Private function CheckFile ()
  67. {
  68. $File= $ _ FILES [$ this->InputName];
  69. Foreach ($ file ['name'] as $Key=>$ Name)
  70. {
  71. If (! Empty ($ name) // The file is not empty.
  72. {
  73. $Type= $ File ['type'] [$ key];
  74. $Size= $ File ['size'] [$ key];
  75. $Error= $ File ['error'] [$ key];
  76. /* Confirm the list of file types allowed to be uploaded */
  77. If (is_array ($ this->FileType [0])
  78. {
  79. $File_type= $ This->FileType [$ key];
  80. }
  81. Else
  82. {
  83. $File_type= $ This->FileType;
  84. }
  85. /* Determine the maximum size of the uploaded file */
  86. If (is_array ($ this->FileMaxSize ))
  87. {
  88. $File_max_size_key=Explode('/', $ Type );
  89. $File_max_size_key= $ File_max_size_key [0];
  90. If (is_array ($ this->FileMaxSize [0])
  91. {
  92. $File_max_size= $ This->FileMaxSize [$ key] [$ file_max_size_key];
  93. }
  94. Else
  95. {
  96. $File_max_size= $ This->FileMaxSize [$ file_max_size_key];
  97. }
  98. }
  99. Else
  100. {
  101. $File_max_size= $ This->FileMaxSize;
  102. }
  103. /* File error */
  104. If ($ error>0)
  105. {
  106. Die ($ name. $ this->NoteFileFalse );
  107. }
  108. /* The file size exceeds the maximum size of the uploaded file */
  109. If ((! Is_null ($ file_max_size) & $ size>$ File_max_size) | ($Size= 0 ))
  110. {
  111. Die ($ name. $ this->NoteFileSize );
  112. }
  113. /* The file type does not match */
  114. If (! In_array ($ type, $ file_type ))
  115. {
  116. Die ($ name. $ this->NoteFileType );
  117. }
  118. }
  119. }
  120. }
  121. /* Get the file suffix */
  122. Private function GetSuffix ($ fileName)
  123. {
  124. Return substr ($ fileName, strrpos ($ fileName ,"."));
  125. }
  126. /* Create a path if the path does not exist */
  127. Private function CreatePath ($ filePath)
  128. {
  129. If (! File_exists ($ filePath ))
  130. {
  131. Mkdir ($ filePath );
  132. }
  133. }
  134. }

Usage of the PHP Generic file Upload class: Next we will illustrate the calling method of this class using the example given at the beginning of this article (haha, the call is very convenient ):

$ Upload_obj = new Upload (); // object to be uploaded

$ Upload_obj-> InputName = 'upload _ test'; // control name of the file upload domain

$ Upload_obj-> FileType = array ('image/jpeg ', 'image/png'), array ('audio/mpeg ', 'Video/x-msvideo'); // specifies the file type that can be uploaded.

$ Upload_obj-> FileMaxSize = array ('image' => 100*1024, 'Audio' => 2*1024*1024, 'video' => 2*1024*1024 );

$ Upload_obj-> FileSavePath = array ('upload/files/s/', 'upload/files/z /');

$ Upload_obj-> FileSaveName = time ();

$ Upload_obj-> NoteFileFalse = 'File error ';

$ Upload_obj-> NoteFileType = 'File type mismatch ';

$ Upload_obj-> NoteFileSize = 'File size exceeded ';

$ File_save_full_name = $ upload_obj-> UploadFile (); // upload and obtain the full name of the file (basic name and extension) (if multiple files are uploaded, they are in the array format) (The full name is used to store information in the database)

Summary: This allows you to easily upload several files. In fact, the PHP Generic file Upload class uses the PHP Group file upload. Note that after the control name is added, do not forget to add []. the advantage is that when multiple files are uploaded, they do not need to be cyclically or upload one by one at the call layer. Therefore, our applications are relaxed.


For example, if you can upload an exhibit and upload a thumbnail for it, the restricted type of the thumbnail file may be jpg, gif, png, etc ,...

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.