博主信息
博文 52
粉丝 1
评论 1
访问量 48783
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
定义文件上传类 Upload.php
小丑0o鱼
原创
670人浏览过

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());
批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学