Blogger Information
Blog 52
fans 1
comment 1
visits 38815
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
定义文件上传类 Upload.php
小丑0o鱼
Original
487 people have browsed it

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. }
  87. 2.上传页 index.html
  88. <form action="doupload.php" method="post" enctype="multipart/form-data">
  89. <input type="file" name="file" id="file">
  90. <button type="submit" id="btn">上传</button>
  91. </form>
  92. 3.处理上传 doupload.php
  93. require 'Upload.php';
  94. $path = './uploads/';
  95. $size = 5242880;
  96. $type = ['image/png','image/jpeg','image/gif','image/jpg','image/wbmp'];
  97. $upload = new Upload($path, $size, $type);
  98. $file = $_FILES['file'];
  99. if ( $info = $upload->uploadOne($file)) {
  100. echo '上传成功' . $info;
  101. } else {
  102. echo $upload->getError();
  103. // 错误写入文件
  104. file_put_contents('log.text', $upload->getError());
Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post