Multi-File upload-PHP

Source: Internet
Author: User
Multi-File upload-PHP
  1. Define (' ROOT ', ' D:/program files/www/test/');
  2. Class files_tool{
  3. protected static $allowExt =array ('. jpg ', '. jpeg ', '. png ', '. gif ', '. bmp ', '. svg ', '. chm ', '. pdf ', '. zip ', '. rar ', '. . Gz ', '. bzip2 ', '. ppt ', '. Doc ');
  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 value of upload_max_filesize option limit 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, file is only partially uploaded ',
  11. 4=> ' File upload failed, no files were uploaded ',
  12. 5=> ' File upload failed, not allowed suffix ',
  13. 6=> ' File upload failed and the Temp folder could not be found. PHP 4.3.10 and PHP 5.0.3 introduced ',
  14. 7=> ' File upload failed, file write failed. PHP 5.1.0 introduced ',
  15. 8=> ' File upload failed, did not receive the form field name ',
  16. 9=> ' File upload failed, error unknown '
  17. );
  18. public static function upload ($name) {
  19. Detects if the name of the form field is received
  20. if (!isset ($_files[$name])) {
  21. Self:: $wrong []=8;
  22. return false;
  23. }
  24. 3-D arrays are simplified into 2-d arrays
  25. $files =array_shift ($_files);
  26. Get suffix
  27. $files =self::get_ext ($files);
  28. Number of files processed
  29. $n =count ($files [' name ']);
  30. for ($i =0; $i < $n; $i + +) {
  31. See if the current file has an error message, 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. View the current file's suffix, allow, if not allowed, 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. Filename
  44. $name =self::rand_name ();
  45. Suffix
  46. $ext = $files [' name '] [$i];
  47. File location
  48. $path = $dir. $name. $ext;
  49. Move temporary file, if failed, skip current file
  50. if (!move_uploaded_file ($files [' tmp_name '] [$i], $path)) {
  51. Self:: $wrong [$i]=9;
  52. Continue
  53. }
  54. Deposit Path
  55. Self:: $path [$i +1]=strtr ($path, Array (root=> "));
  56. }
  57. Return self:: $path;
  58. }
  59. Ways to get suffixes
  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 a path with a 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]= '. $k. "Self:: $error [$k];
  85. }
  86. Return self:: $wrong;
  87. }
  88. }
Copy Code
  • 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.