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. }

更多相关文章

  1. php之封装上传文件函数
  2. 【PHP文件操作】文件目录操作基本操作及文件的上传和下载
  3. 开发文件上传功能稍不注意就会引发安全漏洞
  4. 使用 IntraWeb (24) - 基本控件之 TIWFileUploader、TIWFile
  5. 文件下载函数
  6. 如何用nodeJs向别的服务器上传文件发送formData数据?
  7. h5图片展示和ajax上传
  8. Spring Boot - axios upload file(带请求头上传文件,非前后端分离)
  9. Spring Boot - XMLHttpRequest Upload(上传文件,非前后端分离)

随机推荐

  1. Android的BroadcastReciver收不到Broadca
  2. android aar 使用
  3. Android 打造编译时注解解析框架 这只是
  4. android 权限大全总库
  5. Android 源码下载
  6. ANDROID音频系统散记之一:A2dpAudioInterf
  7. 相对布局(RelativeLayout)
  8. LinearLayout 属性详解
  9. [Android 数据库] Android数据库总结
  10. Android(安卓)studio 入门教程(案例)