博客列表 >PHP文件上传综合示例

PHP文件上传综合示例

大A
大A原创
2020年05月12日 03:57:07720浏览

HTML代码

  1. <?php
  2. require('fileup.php');
  3. if ($_FILES) :
  4. $file = new Fileup($_FILES, 'file');
  5. $result = $file->upload('uploadfile', 'image');
  6. file_ex($result);
  7. show($result);
  8. endif;
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>文件上传</title>
  16. </head>
  17. <body>
  18. <form action="" method="post" enctype="multipart/form-data">
  19. <fieldset>
  20. <legend>单文件上传</legend>
  21. <input type="hidden" name="MAX_FILE_SIZE" value="700000">
  22. <input type="file" name="file[]">
  23. <input type="file" name="file[]">
  24. <input type="file" name="file[]">
  25. <input type="file" name="file[]">
  26. <button>上传</button>
  27. </fieldset>
  28. </form>
  29. <form action="" method="post" enctype="multipart/form-data">
  30. <fieldset>
  31. <legend>多文件上传</legend>
  32. <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  33. <input type="file" name="file[]" multiple>
  34. <button>上传</button>
  35. </fieldset>
  36. </form>
  37. </body>
  38. </html>

文件上传类和异常处理类

  1. <?php
  2. /**
  3. * 文件上传类
  4. */
  5. class Fileup
  6. {
  7. private $file_name, $file_tmp, $file_type, $file_error;
  8. public function __construct($file, $name)
  9. {
  10. $this->file_name = $file[$name]['name'];
  11. $this->file_type = $file[$name]['type'];
  12. $this->file_tmp = $file[$name]['tmp_name'];
  13. $this->file_error = $file[$name]['error'];
  14. }
  15. /**
  16. * 上传文件到指定目录,并可以过滤上传的文件类型
  17. *
  18. * @param [string] $path 上传文件在服务器保存的路径
  19. * @param [string] $type 指定上传的文件类型
  20. * @return array 返回关联二维数组[['name','error','url']...]
  21. */
  22. public function upload($path, $type = null)
  23. {
  24. $result = [];
  25. foreach ($this->file_error as $key => $value) {
  26. if ($value === 0) :
  27. if ($type && strstr($this->file_type[$key], '/', true) !== $type) :
  28. $result[] = [
  29. 'name' => $this->file_name[$key],
  30. 'error' => 8,
  31. 'url' => null
  32. ];
  33. continue;
  34. endif;
  35. $a = $this->file_tmp[$key];
  36. $b = "$path/" . md5(time() + rand(0, 999))
  37. . '.' . pathinfo($this->file_name[$key])['extension'];
  38. if (move_uploaded_file($a, $b)) :
  39. $result[] = [
  40. 'name' => $this->file_name[$key],
  41. 'error' => UPLOAD_ERR_OK,
  42. 'url' => $b
  43. ];
  44. else :
  45. $result[] = [
  46. 'name' => $this->file_name[$key],
  47. 'error' => UPLOAD_ERR_CANT_WRITE,
  48. 'url' => null
  49. ];
  50. endif;
  51. else :
  52. $result[] = [
  53. 'name' => $this->file_name[$key],
  54. 'error' => $value,
  55. 'url' => null
  56. ];
  57. endif;
  58. }
  59. return $result;
  60. }
  61. }
  62. /**
  63. * 文件上传异常类
  64. */
  65. class File_exception extends Exception
  66. {
  67. public function __toString()
  68. {
  69. return '错误代码(' . $this->code . '),' . $this->message . '。<hr>';
  70. }
  71. }
  72. //返回异常并显示出来
  73. function file_ex($result)
  74. {
  75. foreach ($result as $value) {
  76. try {
  77. switch ($value['error']) {
  78. case UPLOAD_ERR_INI_SIZE:
  79. throw new File_exception('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', 1);
  80. break;
  81. case UPLOAD_ERR_FORM_SIZE:
  82. throw new File_exception('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', 2);
  83. break;
  84. case UPLOAD_ERR_PARTIAL:
  85. throw new File_exception('文件只有部分被上传', 3);
  86. break;
  87. case UPLOAD_ERR_NO_TMP_DIR:
  88. throw new File_exception('找不到临时文件夹', 6);
  89. break;
  90. case UPLOAD_ERR_CANT_WRITE:
  91. throw new File_exception('文件写入失败', 7);
  92. break;
  93. case 8:
  94. throw new File_exception('文件类型错误', 8);
  95. break;
  96. }
  97. } catch (File_exception $e) {
  98. echo $value['name'] . ':' . $e;
  99. }
  100. }
  101. }
  102. //预览上传成功的图片
  103. function show($result)
  104. {
  105. foreach ($result as $value) {
  106. if ($value['error'] === UPLOAD_ERR_OK) :
  107. $name = $value['name'];
  108. $v_url = $value['url'];
  109. $html = <<< hh
  110. <div style="width:22%;float:left;margin-right:3%"><div>$name</div><img style="width:100%" src="$v_url" alt=""></div>
  111. hh;
  112. echo $html;
  113. endif;
  114. }
  115. echo '<div style="clear:both"></div>';
  116. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议