Multifile upload-PHP

Source: Internet
Author: User
Multifile upload-PHP
Multifile upload-PHP

  1. Define ('root', 'd:/Program Files/www/test /');
  2. Class Files_Tool {
  3. Protected static comment ');
  4. Public static $ wrong = array ();
  5. Public static $ path = array ();
  6. Protected static $ error = array (
  7. 0 => 'File Upload failed, no error occurred, file upload succeeded ',
  8. 1 => 'File Upload failed. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini ',
  9. 2 => 'File Upload failed. The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form ',
  10. 3 => 'File Upload failed, only part of the file is upload ',
  11. 4 => 'File Upload failed, no file was upload ',
  12. 5 => 'File Upload failed, suffix not allowed ',
  13. 6 => 'File Upload failed. the temporary folders. PHP 4.3.10 and PHP 5.0.3 cannot be found ',
  14. 7 => 'File Upload failed, file writing failed. PHP 5.1.0 introduced ',
  15. 8 => 'File Upload failed. the NAME of the form field is not received ',
  16. 9 => 'File Upload failed, error unknown'
  17. );
  18. Public static function upload ($ name ){
  19. // Check whether the NAME of the form field is received
  20. If (! Isset ($ _ FILES [$ name]) {
  21. Self: $ wrong [] = 8;
  22. Return false;
  23. }
  24. // The 3-dimensional array is simplified to a 2-dimensional array.
  25. $ Files = array_shift ($ _ FILES );
  26. // Obtain the suffix
  27. $ Files = self: get_Ext ($ files );
  28. // Number of file processing times
  29. $ N = count ($ files ['name']);
  30. For ($ I = 0; $ I <$ n; $ I ++ ){
  31. // Check whether the current file has an error message. If yes, skip the current file and process the next file.
  32. If ($ files ['error'] [$ I]! = 0 ){
  33. Self: $ wrong [$ I + 1] = $ files ['error'] [$ I];
  34. Continue;
  35. }
  36. // Check whether the suffix of the current file is allowed. if not, skip the current file.
  37. If (! In_array ($ files ['name'] [$ I], self: $ allowExt )){
  38. Self: $ wrong [$ I + 1] = 5;
  39. Continue;
  40. }
  41. // Path
  42. $ Dir = self: time_Dir ();
  43. // File name
  44. $ Name = self: rand_Name ();
  45. // Suffix
  46. $ Ext = $ files ['name'] [$ I];
  47. // File location
  48. $ Path = $ dir. $ name. $ ext;
  49. // Move the temporary file. if it fails, the current file will be skipped.
  50. If (! Move_uploaded_file ($ files ['tmp _ name'] [$ I], $ path )){
  51. Self: $ wrong [$ I] = 9;
  52. Continue;
  53. }
  54. // Storage path
  55. Self: $ path [$ I + 1] = strtr ($ path, array (ROOT => ''));
  56. }
  57. Return self: $ path;
  58. }
  59. // Obtain the suffix
  60. Protected static function get_Ext ($ arr ){
  61. If (! Is_array ($ arr) |! Isset ($ arr ['name']) {return false ;}
  62. Foreach ($ arr ['name'] as $ k => $ v ){
  63. $ Arr ['name'] [$ k] = strtolower (strrchr ($ v ,'.'));
  64. }
  65. Return $ arr;
  66. }
  67. // Generate path by date
  68. Protected static function time_Dir (){
  69. $ Dir = ROOT. 'data/images/'. date ('Y/m/d/', time ());
  70. If (! Is_dir ($ dir )){
  71. Mkdir ($ dir, 0777, true );
  72. }
  73. Return $ dir;
  74. }
  75. // Generate random file names
  76. Protected static function rand_Name (){
  77. $ Str = str_shuffle ('1234567890qwertyuiopasdfghjklzxcvbnm ');
  78. $ Str = substr ($ str, 0, 6 );
  79. Return $ str;
  80. }
  81. // Error interface
  82. Public static function errors (){
  83. Foreach (self ::$ wrong as $ k => $ v ){
  84. Self: $ wrong [$ k] = 'quarter '. $ k.'. self: $ error [$ k];
  85. }
  86. Return self: $ wrong;
  87. }
  88. }

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.