博客列表 >PHP图片文件批量上传

PHP图片文件批量上传

零龙
零龙原创
2020年08月10日 04:16:09846浏览

PHP图片文件批量上传

示例:

  1. <?php
  2. error_reporting(E_ALL);
  3. //printf('<pre>%s</pre>',print_r($_FILES,true));
  4. class UploadException extends EXception
  5. {
  6. public function __toString()
  7. {
  8. return <<< UPLOAD
  9. <table>
  10. <tr>
  11. <td>编号</td>
  12. <td>信息</td>
  13. <td>文件</td>
  14. <td>行号</td>
  15. </tr>
  16. <tr>
  17. <td>$this->code</td>
  18. <td>$this->message</td>
  19. <td>$this->file</td>
  20. <td>$this->line</td>
  21. </tr>
  22. </table>
  23. <style>
  24. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  25. td {border:1px solid black;padding: 5px;}
  26. tr:first-of-type {background-color:#eee;}
  27. tr:last-of-type td {color: coral;}
  28. </style>
  29. UPLOAD;
  30. }
  31. }
  32. try {
  33. foreach($_FILES as $file){
  34. foreach($_FILES['ufile']['error'] as $k=>$v)
  35. {
  36. $ufiles=['name'=>$_FILES['ufile']['name'][$k],
  37. 'type'=>$_FILES['ufile']['type'][$k],
  38. 'tmp_name'=>$_FILES['ufile']['tmp_name'][$k],
  39. 'error'=>$_FILES['ufile']['error'][$k],
  40. 'size'=>$_FILES['ufile']['size'][$k]
  41. ];
  42. if($ufiles['error']=== 0)
  43. {
  44. $fileType =$ufiles['type'] ?? null;
  45. //判断类型
  46. $type = strstr($fileType,'/',true);//对文件类型进行切割。加上true取/前面的
  47. if(!is_null($fileType))
  48. {
  49. echo $type;
  50. if($type !== 'image') throw new UploadException('文件类型错误',8);
  51. }
  52. //判断临时文件
  53. $tmplFileName = $ufiles['tmp_name'] ?? NULL; //临时文件名
  54. //1.首先临时文件是存在的并且必须是通过POST上传的
  55. // 2.将文件从临时目录移动到目标目录中:uploads
  56. //move_uploaded_file(临时文件,目标文件)
  57. if($tmplFileName && is_uploaded_file($tmplFileName))
  58. {
  59. $originFileName = $ufiles['name'] ?? null;
  60. //获取上传的文件名
  61. $newFileName = strstr($originFileName,'.');
  62. //分割获取文件的后缀如:.jpg/.png/.gif/...
  63. $destFileName = 'uploads/'.md5(time().mt_rand(1,100)).$newFileName ?? null;
  64. //生成存储图片的地址$destFileName
  65. //destFileName 设置上传文件的地址
  66. //如果move_uploaded_file执行成功
  67. if(move_uploaded_file($tmplFileName, $destFileName ))
  68. {
  69. echo '<p>'. $ufiles['name'].'上传成功';
  70. //预览图片
  71. echo "<img src='{$destFileName}' width=200>";
  72. }
  73. else
  74. {
  75. echo '上传失败';
  76. }
  77. }
  78. }
  79. else
  80. {
  81. $errorCode = $ufiles['error'] ?? null;
  82. // 使用系统常量进行判断
  83. if ($errorCode > UPLOAD_ERR_OK)
  84. {
  85. switch($errorCode)
  86. {
  87. case UPLOAD_ERR_INI_SIZE:
  88. throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值',1);
  89. break;
  90. case UPLOAD_ERR_FORM_SIZE:
  91. throw new UploadException('文件大小超过表单中`MAX_FILE_SIZE`指定的值',2);
  92. break;
  93. case UPLOAD_ERR_PARTIAL:
  94. throw new UploadException('文件只有部分被上传',3);
  95. break;
  96. case UPLOAD_ERR_NO_FILE:
  97. throw new UploadException('没有文件被上传',4);
  98. break;
  99. case UPLOAD_ERR_NO_TMP_DIR:
  100. throw new UploadException('找不到临时文件夹',5);
  101. break;
  102. case UPLOAD_ERR_CANT_WRITE:
  103. throw new UploadException('文件写入失败',6);
  104. break;
  105. default:
  106. throw new UploadException('未知错误',7);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. catch (UploadException $e) {
  114. echo $e;
  115. }
  116. ?>
  117. <!DOCTYPE html>
  118. <html lang="en">
  119. <head>
  120. <meta charset="UTF-8">
  121. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  122. <title>多文件上传</title>
  123. </head>
  124. <body>
  125. <form action="" method="POST" enctype="multipart/form-data">
  126. <fieldset>
  127. <legend>多文件上传</legend>
  128. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="500000"> -->
  129. <!-- MAX_FILE_SIZE 设置上传文件的大小 -->
  130. <input type="file" name="ufile[]" id ="ufile" multiple="multiple">
  131. <button>上传</button>
  132. </fieldset>
  133. </form>
  134. </body>
  135. </html>

示例图:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议