Fully encapsulated php file Upload class

Source: Internet
Author: User
Tags php file upload
Fully encapsulated php file Upload class

  1. Class FileUpload {
  2. Private $ filepath; // specifies the path to save the uploaded file
  3. Private $ allowtype = array ('GIF', 'jpg ', 'PNG', 'jpeg '); // specifies the type of the file to be uploaded.
  4. Private $ maxsize = 1000000; // the maximum allowable file length is 1 MB.
  5. Private $ israndname = true; // whether to rename randomly. if the parameter is set to false, the original file name is used.
  6. Private $ originName; // source file name
  7. Private $ tmpFileName; // temporary file name
  8. Private $ fileType; // file type
  9. Private $ fileSize; // file size
  10. Private $ newFileName; // new file name
  11. Private $ errorNum = 0; // error code
  12. Private $ errorMess = ""; // used to provide error reports
  13. // Used for initial conversion of uploaded files
  14. // 1. specify the upload path, 2, the acceptable type, 3, size limit, 4, whether to use the random file name
  15. // You do not need to transmit parameters by position. the values provided by the following parameters do not need to provide the values of the previous parameters.
  16. Function _ construct ($ options = array ()){
  17. Foreach ($ options as $ key => $ val ){
  18. $ Key = strtolower ($ key );
  19. // Check whether the subscript of the array in the user parameter is the same as the member attribute name
  20. If (! In_array ($ key, get_class_vars (get_class ($ this )))){
  21. Continue;
  22. }
  23. $ This-> setOption ($ key, $ val );
  24. }
  25. }
  26. Private function getError (){
  27. $ Str = "an error occurred while uploading the file {$ this-> originName :";
  28. Switch ($ this-> errorNum ){
  29. Case 4: $ str. = "no file is uploaded"; break;
  30. Case 3: $ str. = "The file is only partially uploaded"; break;
  31. Case 2: $ str. = "the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form"; break;
  32. Case 1: $ str. = "the uploaded file exceeds the value of the upload_max_filesize option in php. ini"; break;
  33. Case-1: $ str. = "Last supported types"; break;
  34. Case-2: $ str. = "the file to be uploaded is too large to exceed {$ this-> maxSize} bytes"; break;
  35. Case-3: $ str. = "Upload failed"; break;
  36. Case-4: $ str. = "an error occurred while creating the Upload file directory. please specify the upload Directory Again"; break;
  37. Case-5: $ str. = "the path of the file to be uploaded must be specified"; break;
  38. Default: $ str. = "Unknown error ";
  39. }
  40. Return $ str .'
    ';
  41. }
  42. // Used to check the file upload path
  43. Private function checkFilePath (){
  44. If (empty ($ this-> filepath )){
  45. $ This-> setOption ('errornum',-5 );
  46. Return false;
  47. }
  48. If (! File_exists ($ this-> filepath) |! Is_writable ($ this-> filepath )){
  49. If (! @ Mkdir ($ this-& gt; filepath, 0755 )){
  50. $ This-> setOption ('errornum',-4 );
  51. Return false;
  52. }
  53. }
  54. Return true;
  55. }
  56. // Used to check the file upload size
  57. Private function checkFileSize (){
  58. If ($ this-> fileSize> $ this-> maxsize ){
  59. $ This-> setOPtion ('errornum', '-2 ');
  60. Return false;
  61. } Else {
  62. Return true;
  63. }
  64. }
  65. // Check the file upload type
  66. Private function checkFileType (){
  67. If (in_array (strtolower ($ this-> fileType), $ this-> allowtype )){
  68. Return true;
  69. } Else {
  70. $ This-> setOption ('errornum',-1 );
  71. Return false;
  72. }
  73. }
  74. // Set the name of the uploaded file
  75. Private function setNewFileName (){
  76. If ($ this-> israndname ){
  77. $ This-> setOption ('newfilename', $ this-> proRandName ());
  78. } Else {
  79. $ This-> setOption ('newfilename', $ this-> originName );
  80. }
  81. }
  82. // Set the random file name
  83. Private function proRandName (){
  84. $ FileName = date ("YmdHis"). rand (100,999 );
  85. Return $ fileName. '.'. $ this-> fileType;
  86. }
  87. Private function setOption ($ key, $ val ){
  88. $ This-> $ key = $ val;
  89. }
  90. // Used to upload a file
  91. Function uploadFile ($ fileField ){
  92. $ Return = true;
  93. // Check the file upload path
  94. If (! $ This-> checkFilePath ()){
  95. $ This-> errorMess = $ this-> getError ();
  96. Return false;
  97. }
  98. $ Name = $ _ FILES [$ fileField] ['name'];
  99. $ Tmp_name = $ _ FILES [$ fileField] ['tmp _ name'];
  100. $ Size = $ _ FILES [$ fileField] ['size'];
  101. $ Error = $ _ FILES [$ fileField] ['error'];
  102. If (is_Array ($ name )){
  103. $ Errors = array ();
  104. For ($ I = 0; $ I If ($ this-> setFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {
  105. If (! $ This-> checkFileSize () |! $ This-> checkFileType ()){
  106. $ Errors [] = $ this-> getError ();
  107. $ Return = false;
  108. }
  109. } Else {
  110. $ Error [] = $ this-> getError ();
  111. $ Return = false;
  112. }
  113. If (! $ Return)
  114. $ This-> setFiles ();
  115. }
  116. If ($ return ){
  117. $ FileNames = array ();
  118. For ($ I = 0; $ I If ($ this-> setFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {
  119. $ This-> setNewFileName ();
  120. If (! $ This-> copyFile ()){
  121. $ Errors = $ this-> getError ();
  122. $ Return = false;
  123. } Else {
  124. $ FileNames [] = $ this-> newFileName;
  125. }
  126. }
  127. }
  128. $ This-> newFileName = $ fileNames;
  129. }
  130. $ This-> errorMess = $ errors;
  131. Return $ return;
  132. } Else {
  133. If ($ this-> setFiles ($ name, $ tmp_name, $ size, $ error )){
  134. If ($ this-> checkFileSize () & $ this-> checkFileType ()){
  135. $ This-> setNewFileName ();
  136. If ($ this-> copyFile ()){
  137. Return true;
  138. } Else {
  139. $ Return = false;
  140. }
  141. } Else {
  142. $ Return = false;
  143. }
  144. } Else {
  145. $ Return = false;
  146. }
  147. If (! $ Return)
  148. $ This-> errorMess = $ this-> getError ();
  149. Return $ return;
  150. }
  151. }
  152. Private function copyFile (){
  153. If (! $ This-> errorNum ){
  154. $ Filepath = rtrim ($ this-> filepath ,'/').'/';
  155. $ Filepath. = $ this-> newFileName;
  156. If (@ move_uploaded_file ($ this-> tmpFileName, $ filepath )){
  157. Return true;
  158. } Else {
  159. $ This-> setOption ('errornum',-3 );
  160. Return false;
  161. }
  162. } Else {
  163. Return false;
  164. }
  165. }
  166. // Set content related to $ _ FILES
  167. Private function setFiles ($ name = "", $ tmp_name = '', $ size = 0, $ error = 0 ){
  168. $ This-> setOption ('errornum', $ error );
  169. If ($ error ){
  170. Return false;
  171. }
  172. $ This-> setOption ('originname', $ name );
  173. $ This-> setOption ('tmpfilename', $ tmp_name );
  174. $ ArrStr = explode ('.', $ name );
  175. $ This-> setOption ('filetype', strtolower ($ arrStr [count ($ arrStr)-1]);
  176. $ This-> setOption ('filesize', $ size );
  177. Return true;
  178. }
  179. // Get the name of the uploaded file
  180. Function getNewFileName (){
  181. Return $ this-> newFileName;
  182. }
  183. // If the upload fails, call this method to view the error report.
  184. Function getErrorMsg (){
  185. Return $ this-> errorMess;
  186. }
  187. }

Install, upload files, php

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.