当前位置:Gxlcms > PHP教程 > php普通表单多文件上传的代码

php普通表单多文件上传的代码

时间:2021-07-01 10:21:17 帮助过:9人阅读

  1. /*

  2. * class: 文件上传类
  3. * author: ZMM
  4. * date: 2011.1.20
  5. * email: 304924248@qq.com
  6. * link: http://bbs.it-home.org
  7. */

  8. class Upload {

  9. public $up_ext=array(), $up_max=5210, $up_dir;
  10. private $up_name, $up_rename=true, $up_num=0, $up_files=array(), $up_ret=array();

  11. function __construct($name, $ext=array(), $rename=true) {

  12. if (!empty($name)) {
  13. $this->up_name = $name;
  14. !empty($ext) && $this->up_ext = $ext;
  15. $this->up_rename = $rename;
  16. $this->up_dir = WEBSITE_DIRROOT.
  17. $GLOBALS['cfg_upload_path'];
  18. $this->InitUpload();
  19. } else {
  20. exit('upload文件域名称为空,初始化失败!');
  21. }
  22. }

  23. private function InitUpload() {

  24. if (is_array($_FILES[$this->up_name])) {
  25. $up_arr = count($_FILES[$this->up_name]);
  26. $up_all = count($_FILES[$this->up_name], 1);
  27. $up_cnt = ($up_all - $up_arr) / $up_arr;
  28. for ($i = 0; $i < $up_cnt; $i ++) {
  29. if ($_FILES[$this->up_name]['error'][$i] != 4) {
  30. $this->up_files[] = array(
  31. 'tmp_name' => $_FILES[$this->up_name]['tmp_name'][$i],
  32. 'name' => $_FILES[$this->up_name]['name'][$i],
  33. 'type' => $_FILES[$this->up_name]['type'][$i],
  34. 'size' => $_FILES[$this->up_name]['size'][$i],
  35. 'error' => $_FILES[$this->up_name]['error'][$i]
  36. );
  37. }
  38. }
  39. $this->up_num = count($this->up_files);
  40. } else {
  41. if (isset($_FILES[$this->up_name])) {
  42. $this->up_files = array(
  43. 'tmp_name' => $_FILES[$this->up_name]['tmp_name'],
  44. 'name' => $_FILES[$this->up_name]['name'],
  45. 'type' => $_FILES[$this->up_name]['type'],
  46. 'size' => $_FILES[$this->up_name]['size'],
  47. 'error' => $_FILES[$this->up_name]['error']
  48. );
  49. $this->up_num = 1;
  50. } else {
  51. exit('没找找到需要upload的文件!');
  52. }
  53. }

  54. $this->ChkUpload();

  55. }

  56. private function ChkUpload() {

  57. if (empty($this->up_ext)) {
  58. $up_mime = array('image/wbmp', 'image/bmp', 'image/gif', 'image/pjpeg', 'image/x-png');
  59. foreach ($this->up_files as $up_file) {
  60. $up_allw = false;
  61. foreach ($up_mime as $mime) {
  62. if ($up_file['type'] == $mime) {
  63. $up_allw = true; break;
  64. }
  65. }
  66. !$up_allw && exit('不允许上传'.$up_file['type'].'格式的文件!');

  67. if ($up_file['size'] / 1024 > $this->up_max) {

  68. exit('不允许上传大于 '.$this->up_max.'K 的文件!');
  69. }
  70. }
  71. } else {
  72. foreach ($this->up_files as $up_file) {
  73. $up_ext = end(explode('.', $up_file['name']));

  74. $up_allw = false;

  75. foreach ($this->up_ext as $ext) {
  76. if ($up_ext == $ext) {
  77. $up_allw = true; break;
  78. }
  79. }
  80. !$up_allw && exit('不允许上传.'.$up_ext.'格式的文件!');

  81. if ($up_file['size'] / 1024 > $this->up_max) {

  82. exit('不允许上传大于 '.$this->up_max.'K 的文件!');
  83. }
  84. }
  85. }

  86. $this->Uploading();

  87. }

  88. private function Uploading() {

  89. if (IO::DIRCreate($this->up_dir)) {
  90. if (chmod($this->up_dir, 0777)) {
  91. if (!empty($this->up_files)) {
  92. foreach ($this->up_files as $up_file) {
  93. if (is_uploaded_file($up_file['tmp_name'])) {
  94. $file_name = $up_file['name'];
  95. if ($this->up_rename) {
  96. $file_ext = end(explode('.', $file_name));
  97. $file_rnd = substr(md5(uniqid()), mt_rand(0, 26), 6);
  98. $file_name = date('ymdHis').'_'.$file_rnd.'.'.$file_ext;
  99. }
  100. $file_name = $this->up_dir.'/'.$file_name;

  101. if (move_uploaded_file($up_file['tmp_name'], $file_name)) {

  102. $this->up_ret[] = str_replace(WEBSITE_DIRROOT, '', $file_name);
  103. } else {
  104. exit('文件上传失败!');
  105. }
  106. }
  107. }
  108. }
  109. } else {
  110. exit('未开启写入权限!');
  111. }
  112. } else {
  113. exit('上传目录创建失败!');
  114. }
  115. }

  116. public function GetUpload() {

  117. return empty($this->up_ret) ? false : $this->up_ret;
  118. }

  119. function __destruct() {}

  120. }
  121. ?>

人气教程排行