Blogger Information
Blog 14
fans 0
comment 0
visits 8794
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传的实例
于星辉
Original
445 people have browsed it

前端

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <form action="userUpload.php" method="post" enctype="multipart/form-data">
  11. 单文件上传<input type="file" name="pic" id="">
  12. <input type="submit" value="上传">
  13. </form>
  14. <form action="userUpload.php" method="post" enctype="multipart/form-data">
  15. 多文件上传<input type="file" name="pic[]" multiple id="">
  16. <input type="submit" value="上传">
  17. </form>
  18. </body>
  19. </html>
  1. <?php
  2. // 文件上传类
  3. /**
  4. * 1 判断上传过程中是否有错误
  5. * 2 验证上传文件的类型是否符合要求
  6. * 3 验证上传文件的大小是否符合要求
  7. * 4 判断上传路径和上传成功后要保存的文件名新名称
  8. * 5 将图片从垃圾目录中移除
  9. * 注意:
  10. * 如果是单文件上传$_FILES['表单名称']则是一个一维数组
  11. * 如果是多文件上传$_FILES['表单名称']则是一个二维数组,数组中name对应着每个上传文件的名称
  12. */
  13. class Upload{
  14. //成员属性
  15. //将是检测上传过程的数组变量
  16. public $file;
  17. public $pic;
  18. public $path;
  19. public $size;
  20. public $type;
  21. public $newImg;
  22. public $pathInfo = array();
  23. //成员方法
  24. //构造方法 初始化成员属性值
  25. function __construct($pic,$path='./upload',$size = 5000000,array $type = array('image/jpeg','image/jpg','image/gif','image/png')){
  26. //初始化赋值
  27. $this->pic = $pic;
  28. $this->file= $_FILES[$pic];
  29. $this->path= $path;
  30. $this->size =$size;
  31. $this->type = $type;
  32. }
  33. //需要一个方法可以在外部调用进行上传文件操作
  34. public function do_upload($mutiple = false){
  35. if($mutiple === false){
  36. //用户使用单文件上传方式
  37. return $this->myExec();
  38. }else{
  39. $return = [];
  40. //用户使用多文件上传方式
  41. foreach($_FILES[$this->pic]['name'] as $k=>$v){
  42. $this->file['name'] = $v;
  43. $this->file['type'] = $_FILES[$this->pic]['type'][$k];
  44. $this->file['error'] = $_FILES[$this->pic]['error'][$k];
  45. $this->file['tmp_name'] = $_FILES[$this->pic]['tmp_name'][$k];
  46. $this->file['size'] = $_FILES[$this->pic]['size'][$k];
  47. $return[]= $this->myExec();
  48. }
  49. return $return;
  50. }
  51. }
  52. //6 制作一个方法将文件上传的过程调用执行
  53. private function myExec(){
  54. //需要将文件上传的5步都执行一次判断是否成功
  55. if($this->fileError() !==true){
  56. return $this->fileError();
  57. }elseif($this->patternType() !== true){
  58. return $this->patterType();
  59. }elseif($this->patternSize() !== true){
  60. return $this->patternSize();
  61. }elseif($this->reNameImage() !== true){
  62. return $this->reNameImage();
  63. }elseif($this->moveImg() !== true){
  64. return $this->moveImg();
  65. }else{
  66. return $this->moveImg();
  67. }
  68. }
  69. // 1 判断上传过程中是否有错误
  70. private function fileError(){
  71. //错误判断
  72. if($this->file['error']){
  73. switch($this->file['error']>0){
  74. case 1:
  75. return '超出PHP.INI 配置文件中的upload_max_filesize设置的值';
  76. case 2:
  77. return '超过了HTML表单中设置的max_file_size的值';
  78. case 3:
  79. return '只有部分文件被上传';
  80. case 4:
  81. return '没有文件上传';
  82. case 6:
  83. return '找不到临时目录';
  84. case 7:
  85. return '写入失败';
  86. }
  87. }
  88. return true;
  89. }
  90. //2 验证上传文件的类型是否符合要求
  91. private function patternType(){
  92. if(!in_array($this->file['type'],$this->type)){
  93. return '类型不合法';
  94. }
  95. return true;
  96. }
  97. // 3 验证上传文件的大小是否符合要求
  98. private function patternSize(){
  99. if($this->file['size']>$this->size){
  100. return '上传文件大小超过了预设的'.$this->size.'byte大小';
  101. }
  102. return true;
  103. }
  104. // 4 判断上传路径和上传成功后要保存的文件名新名称
  105. private function reNameImage(){
  106. //创建保存目录,可能会遇到upload/pic/nihao/
  107. if(!file_exists($this->path)){
  108. mkdir($this->path,0777,true);
  109. }
  110. //获取图片后缀名
  111. $suffix = strrchr($this->file['name'],'.');
  112. //判断名称是否重复
  113. do{
  114. //制作新名称
  115. $this->newImg = md5(time().mt_rand(1,1000).uniqid()).$suffix;
  116. }while(file_exists($this->path.$this->newImg));
  117. return true;
  118. }
  119. // 5 将图片从垃圾目录中移除
  120. private function moveImg(){
  121. if(move_uploaded_file($this->file['tmp_name'],$this->path.$this->newImg)){
  122. $this->pathInfo['pathInfo']= $this->path.$this->newImg;
  123. $this->pathInfo['name']= $this->newImg;
  124. $this->pathInfo['path'] = $this->path;
  125. return $this->pathInfo;
  126. }else{
  127. return '未知错误,请重新上传';
  128. }
  129. }
  130. }
  131. ?>
  1. <?php
  2. //包含上传类,创建上传文件对象
  3. echo '<pre>';
  4. var_dump($_FILES);
  5. include './upload.php';
  6. $upload = new Upload('pic');
  7. $res=$upload->do_upload(true);
  8. var_dump($res);
Correcting teacher:PHPzPHPz

Correction status:qualified

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