首页 > 后端开发 > php教程 > php普通表单多文件上传的代码

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

WBOY
发布: 2016-07-25 09:04:26
原创
960 人浏览过
  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 if ($_FILES[$this->up_name]['error'][$i] != 4) {
  29. $this->up_files[] = array(
  30. 'tmp_name' => $_FILES[$this->up_name]['tmp_name'][$i],
  31. 'name' => $_FILES[$this->up_name]['name'][$i],
  32. 'type' => $_FILES[$this->up_name]['type'][$i],
  33. 'size' => $_FILES[$this->up_name]['size'][$i],
  34. 'error' => $_FILES[$this->up_name]['error'][$i]
  35. );
  36. }
  37. }
  38. $this->up_num = count($this->up_files);
  39. } else {
  40. if (isset($_FILES[$this->up_name])) {
  41. $this->up_files = array(
  42. 'tmp_name' => $_FILES[$this->up_name]['tmp_name'],
  43. 'name' => $_FILES[$this->up_name]['name'],
  44. 'type' => $_FILES[$this->up_name]['type'],
  45. 'size' => $_FILES[$this->up_name]['size'],
  46. 'error' => $_FILES[$this->up_name]['error']
  47. );
  48. $this->up_num = 1;
  49. } else {
  50. exit('没找找到需要upload的文件!');
  51. }
  52. }
  53. $this->ChkUpload();

  54. }
  55. private function ChkUpload() {

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

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

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

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

  86. }
  87. private function Uploading() {

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

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

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

  119. }
  120. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板