PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 0514文件上传

0514文件上传

千山暮雪
千山暮雪 原创
2021年05月17日 18:28:19 601浏览


1.定义文件上传类 Upload.php

  1. class Upload
  2. {
  3. private $path;
  4. private $size;
  5. private $type;
  6. private $error;
  7. public function __construct($path, $size, $type)
  8. {
  9. $this->path = $path;
  10. $this->size = $size;
  11. $this->type = $type;
  12. }
  13. // 返回错误信息
  14. public function getError()
  15. {
  16. return $this->error;
  17. }
  18. // 验证上传是否有误
  19. public function checkError($files)
  20. {
  21. // 1. 验证错误号
  22. if ($files['error'] != 0) {
  23. switch ($files['error']) {
  24. case 1:
  25. $this->error = '文件大小超过了php.ini中允许的最大值,最大值是:'.ini_get('upload_max_filesize');
  26. return false;
  27. case 2:
  28. $this->error = '文件大小超过了表单允许的最大值';
  29. return false;
  30. case 3:
  31. $this->error = '只有部分文件上传成功';
  32. return false;
  33. case 4:
  34. $this->error = '没有文件上传成功';
  35. return false;
  36. case 6:
  37. $this->error = '找不到临时文件';
  38. return false;
  39. case 7:
  40. $this->error = '文件写入失败';
  41. return false;
  42. default:
  43. $this->error = '未知错误';
  44. return false;
  45. }
  46. }
  47. // 2. 验证格式
  48. $info = finfo_open(FILEINFO_MIME_TYPE);
  49. $mime = finfo_file($info, $files['tmp_name']);
  50. if (!in_array($mime, $this->type)) {
  51. $this->error = '只能上传'.implode(',', $this->type).'格式';
  52. return false;
  53. }
  54. // 3. 验证大小
  55. if ($files['size'] > $this->size) {
  56. $this->error = '文件大小不能超过HTTP POST上传的';
  57. return false;
  58. }
  59. if (!is_uploaded_file($files['tmp_name'])) {
  60. $this->error = '文件不是HTTP POST上传的';
  61. return false;
  62. }
  63. return true;
  64. }
  65. // 文件上传
  66. public function uploadOne($files)
  67. {
  68. if ($this->checkError($files)) { // 没有错误就上传
  69. $folderName = date('Y-m-d'); // 文件夹名
  70. $folderPath = $this->path . $folderName; // 文件夹路径
  71. if (!is_dir($this->path))
  72. mkdir($this->path,0777);
  73. if (!is_dir($folderPath))
  74. mkdir($folderPath,0777);
  75. $fileName = uniqid('', true) . strrchr($files['name'], '.'); //文件名
  76. $filePath = "$folderPath / $fileName";
  77. if (move_uploaded_file($files['tmp_name'], $filePath)) {
  78. return "${$folderPath} / {$fileName}";
  79. } else {
  80. $this->error = '上传失败<br>';
  81. return false;
  82. }
  83. }
  84. return false;
  85. }
  86. }

2.上传页 index.html

  1. <form action="doupload.php" method="post" enctype="multipart/form-data">
  2. <input type="file" name="file" id="file">
  3. <button type="submit" id="btn">上传</button>
  4. </form>

3.处理上传 doupload.php

  1. require 'Upload.php';
  2. $path = './uploads/';
  3. $size = 5242880;
  4. $type = ['image/png','image/jpeg','image/gif','image/jpg','image/wbmp'];
  5. $upload = new Upload($path, $size, $type);
  6. $file = $_FILES['file'];
  7. if ( $info = $upload->uploadOne($file)) {
  8. echo '上传成功' . $info;
  9. } else {
  10. echo $upload->getError();
  11. // 错误写入文件
  12. file_put_contents('log.text', $upload->getError());
  13. }
上一条: mysql基础知识 下一条: 08-Vue_组件交互
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议