PHP实现多图片上传类实例

这篇文章主要介绍了PHP实现多图片上传类实例,该类文件注释详细功能实用且具有一定的扩展性,需要的朋友可以参考下

本文所述为一个实用的PHP多图片文件上传类,其支持gif、jpg、jpeg、pjpeg、png格式的多图片上传功能,类中还可限制图片类型、上传图片的大小、设置上传目录、一些提交判断等功能。此外该类并不局限于图片的上传,也可以上传TXT/RAR等文件类型,只是需要对代码进行一下修改,感兴趣的读者可以自己尝试一下。

php多图片上传类完整功能代码如下:

  1. <?php
  2. class more_file_upload{
  3. const FILE_PATH = '/uploadfile/'; //默认文件上传的目录,请自行修改
  4. var file_type;
  5. var file_type_array;
  6. var file_type_real_array;
  7. var file_type_string;
  8. var file_name;
  9. var file_size;
  10. var file_tmp_name;
  11. var file_error;
  12. var handledate;
  13. static totalsize = 0;
  14. function __construct(file_name, file_error, file_size, file_tmp_name, file_type){
  15. this -> handledate = date('m-d-Y');
  16. if (!emptyempty(file_name)){
  17. this -> file_name = file_name;
  18. this -> file_error = file_error;
  19. this -> file_size = file_size;
  20. this -> file_tmp_name = file_tmp_name;
  21. this -> file_type = file_type;
  22. this -> file_type_array = array('/', 'image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png');
  23. this -> file_type_real_array = array(0.1, 'jpg' => 74707370, 'gif' => 7173, 'bmp' => 6677, 'png' => 807871);
  24. this -> show_execute_message(this -> file_error, this -> file_name, this -> file_type, this -> file_size);
  25. }
  26. }
  27. function __destruct(){
  28. this -> file_name = NULL;
  29. this -> file_error = NULL;
  30. this -> file_size = NULL;
  31. this -> file_tmp_name = NULL;
  32. this -> file_type = NULL;
  33. self :: totalsize = 0;
  34. }
  35. function show_execute_message(smfileerror, smfilename, smfiletype, smfilesize){
  36. if(smfileerror > 0){
  37. switch (smfileerror){
  38. case 1: smfilemessage = '<strong>文件超过服务器的约定大小!</strong>';
  39. break;
  40. case 2: smfilemessage = '<strong>文件超过指定的文件大小!</strong>';
  41. break;
  42. case 3: smfilemessage = '<strong>文件只上传了部分!</strong>';
  43. break;
  44. case 4: echo "this->file_name " . '文件上传失败!<br/>';
  45. 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. self :: 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文件中,并保存文件名为:more_upload_file.class.php,在使用时include该文件即可。