PHP多文件上传类实例

这篇文章主要介绍了PHP多文件上传类,实例分析了php实现多文件上传的原理与实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下.

本文实例讲述了PHP多文件上传类,分享给大家供大家参考,具体如下:

  1. <?php
  2. /*
  3. PHP多文件上传类
  4. 修改:Linvo 2008-2-15
  5. */
  6. class more_file_upload{
  7. const FILE_PATH='../upfileclass/uploadfile/';
  8. var $file_type;
  9. var $file_type_array;
  10. var $file_type_real_array;
  11. var $file_type_string;
  12. var $file_name;
  13. var $file_size;
  14. var $file_tmp_name;
  15. var $file_error;
  16. var $handledate;
  17. var $totalsize=0;
  18. function __construct($file_name,$file_error,$file_size,$file_tmp_name,$file_type){
  19. $this->handledate=date('m-d-Y');
  20. if (!emptyempty($file_name)){
  21. $this->$file_name = $file_name;
  22. $this->$file_error = $file_error;
  23. $this->$file_size = $file_size;
  24. $this->$file_tmp_name = $file_tmp_name;
  25. $this->$file_type = $file_type;
  26. $this->$file_type_array = array('/', 'image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png');
  27. $this->$file_type_real_array = array(0.1, 'jpg'=>74707370, 'gif'=>7173, 'bmp'=>6677, 'png'=>807871);
  28. $this->show_execute_message($this->$file_error,$this->$file_name,$this->$file_type,$this->$file_size);
  29. }
  30. }
  31. function __destruct(){
  32. $this->$file_name = NULL;
  33. $this->$file_error = NULL;
  34. $this->$file_size = NULL;
  35. $this->$file_tmp_name = NULL;
  36. $this->$file_type = NULL;
  37. $this->$totalsize = 0;
  38. }
  39. function show_execute_message($smfileerror,$smfilename,$smfiletype,$smfilesize){
  40. if($smfileerror>0){
  41. switch ($smfileerror){
  42. case 1: $smfilemessage='<strong>文件超过服务器的约定大小!</strong>';break;
  43. case 2: $smfilemessage ='<strong>文件超过指定的文件大小!</strong>';break;
  44. case 3: $smfilemessage='<strong>文件只上传了部分!</strong>';break;
  45. case 4: echo "$this->$file_name ".'文件上传失败!<br/>';break;
  46. }
  47. self::__destruct();
  48. }else{
  49. $smfiletypeflag = array_search($smfiletype,$this->$file_type_array);
  50. //进行真实格式验证
  51. if($smfiletypeflag != false){
  52. $file = fopen($this->$file_tmp_name, "rb");
  53. $bin = fread(file, 10);
  54. fclose($file);
  55. $strInfo = @unpack("c10chars", $bin);
  56. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  57. $smfiletypeflag = array_search($typeCode, $this->$file_type_real_array);
  58. if($smfiletypeflag == false){ //判断是否是png图片
  59. $typeCode = intval($strInfo['chars2'].$strInfo['chars3'].$strInfo['chars4']);
  60. $smfiletypeflag = array_search($typeCode, $this->$file_type_real_array);
  61. if($smfiletypeflag == false){ //判断是否是jpg图片
  62. $$typeCode = intval($strInfo['chars7'].$strInfo['chars8'].$strInfo['chars9'].$strInfo['chars10']);
  63. $smfiletypeflag = array_search($typeCode, $this->$file_type_real_array);
  64. }
  65. }
  66. }
  67. if($smfiletypeflag == false){
  68. $smfilemessage='<strong>文件类型不对,请核实!</strong>';
  69. self::__destruct();
  70. }else{
  71. $resflag = $$this->move_file($$this->$file_tmp_name,$this->$file_name);
  72. if ($resflag == 1){
  73. $smfilemessage = '文件上传成功!';
  74. $this->totalsize += intval($smfilesize);
  75. self::__destruct();
  76. }else{
  77. $smfilemessage = '<strong>文件上传失败!</strong>';
  78. self::__destruct();
  79. }
  80. }
  81. }
  82. $$smfilesizeformat = $this->size_BKM($smfilesize);
  83. echo '<tr>
  84. <td align="left" >'.$smfilename.'</td>
  85. <td align="center" >'.$smfiletype.'</td>
  86. <td align="center" >'.$smfilesizeformat.'</td>
  87. <td align="center" >'.smfilemessage.'</td>
  88. </tr>';
  89. }
  90. function move_file($mvfiletmp,$mvfilename){ //移动文件
  91. $mvfilenamearr = explode('.',basename($mvfilename));
  92. $mvfilenamearr[0] = $this->rand_string();
  93. $mvfilename = implode('.',$mvfilenamearr);
  94. if (is_uploaded_file($mvfiletmp)){
  95. $uploadfile = self::FILE_PATH."$mvfilename";
  96. $result = move_uploaded_file($mvfiletmp,$uploadfile);
  97. return $result;
  98. }
  99. }
  100. function rand_string(){
  101. $string = md5(uniqid(rand().microtime()));
  102. return $string;
  103. }
  104. function size_BKM($size){ // B/KB/MB单位转换
  105. if($size < 1024)
  106. {
  107. $size_BKM = (string)$size . " B";
  108. }
  109. elseif($size < (1024 * 1024))
  110. {
  111. $size_BKM = number_format((double)($size / 1024), 1) . " KB";
  112. }else
  113. {
  114. $size_BKM = number_format((double)($size / (1024*1024)),1)." MB";
  115. }
  116. return $size_BKM;
  117. }
  118. }
  119. ?>

希望本文所述对大家的php程序设计有所帮助。