PHP General file Upload class specific parsing _php tutorial

Source: Internet
Author: User
We'll give you a detailed explanation below.For example: A user can upload an exhibit and upload a thumbnail for the exhibit, then the thumbnail file may have a limit type of jpg,,gif,png and so on, while the exhibit file limit type may be mov,avi,mpeg, and the image size may be limited to 100KB. The audio video size may be limited to 2MB. The class code is as follows:

 
 
  1. Class Upload
  2. {
  3. Public $InputName; File Upload domain control name
  4. /**
  5. * File types allowed for uploading
  6. * The form is array (' image/jpeg ', ' image/png ', ' image/gif ') or an array containing such an array (corresponding to each upload domain control)
  7. */
  8. Public $FileType;
  9. /**
  10. * Maximum upload file size (in bytes)
  11. * format is an array (' image ' => $size, ' audio ' => $size) (which indicates the upload size for each application file type) or contains this An array of class arrays (corresponding to each upload domain control) or a numeric value (indicating that all uploaded files are limited to this size)
  12. */
  13. Public $FileMaxSize;
  14. Public $FileSavePath; File save path (available as an array, representing different upload domains uploading files to different paths)
  15. Public $FileSaveName; File Save name (does not include suffix) (can be an array, indicating different upload domain upload files to save different names)
  16. Public $NoteFileFalse; File Error prompt
  17. Public $NoteFileType; File type mismatch prompt
  18. Public $NoteFileSize; File size exceeded hint
  19. /* Upload the file and return the filename information (with the suffix name) */
  20. Public Function UploadFile ()
  21. {
  22. $this- > checkfile ();//Inspection file
  23. $ file = $_files[$this->inputname];
  24. $ File_save_full_name = Array ();//File Save name (with suffix name)
  25. foreach ($file [' name '] as $key => $name)
  26. {
  27. if (!empty ($name))//file is not empty
  28. {
  29. /* Determine the File save 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 file Save name (without suffix) */
  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 saving */
  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 filename 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. /* Test File */
  66. Private Function Checkfile ()
  67. {
  68. $ file = $_files[$this->inputname];
  69. foreach ($file [' name '] as $key => $name)
  70. {
  71. if (!empty ($name))//file is not empty
  72. {
  73. $ type = $file [' type '] [$key];
  74. $ size = $file [' Size '] [$key];
  75. $ Error = $file [' ERROR '] [$key];
  76. /* OK to allow uploading of file type list */
  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 upload file size */
  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. /* File size exceeds maximum upload file size */
  109. if (!is_null ($file _max_size) && $size > $file _max_size) | | ($ size = = 0))
  110. {
  111. Die ($name. $this->notefilesize);
  112. }
  113. /* 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. }

PHP common File Upload class use: Then the example at the beginning of this article to illustrate the invocation method of the class (hehe, the call is very convenient):

$upload _obj = new Upload (); File Upload Object

$upload _obj->inputname = ' upload_test '; File Upload domain control name

$upload _obj->filetype = Array (' Image/jpeg ', ' image/png '), Array (' Audio/mpeg ', ' Video/x-msvideo ')); Allowed file types to upload

$upload _obj->filemaxsize = Array (' image ' = + * 1024x768, ' audio ' = 2 * 1024x768 * 1024x768, ' 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 does not match ';

$upload _obj->notefilesize = ' file size exceeded ';

$file _save_full_name = $upload _obj->uploadfile (); Upload and get full file name (base name plus extension) (if multiple files are in array form) (full name for storing information in database)

Summary: In this can be easily implemented a number of file upload, in fact, PHP general file upload class in the final analysis of the PHP group file upload, to note that the name of the control is not forgotten to add the [], the advantage is to encounter multiple file uploads do not have to cycle in the call layer or a processing upload, Our applications are also easy to use.


http://www.bkjia.com/PHPjc/446390.html www.bkjia.com true http://www.bkjia.com/PHPjc/446390.html techarticle we will explain the following for you, for example: Users can upload an exhibit and upload a thumbnail for the exhibit, then the thumbnail file limit type may be jpg,,gif,png, etc. ..

  • Related Article

    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.