前端

  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);

更多相关文章

  1. PHP文件上传的各种处理
  2. 删除、修改省名称
  3. uniapp H5图片上传压缩自动旋转
  4. HTML表格案例《商品信息表》
  5. PHP:文件上传上传限制,文件大小不超过5M,文件后缀设置,检查图片合
  6. 【MVC设计模式】PHP文件上传实战案例和MVC设计模式的原理和实现
  7. 0514文件上传
  8. php之封装上传文件函数
  9. 【PHP文件操作】文件目录操作基本操作及文件的上传和下载

随机推荐

  1. 如何使用CSS精灵重复背景图像?
  2. 前端html数组去重的方法
  3. 多文件拖拽上传以及利用Jquery替代HTML5
  4. html5 地理位置定位的时候,想用enableHigh
  5. HTML5小试 双人贪吃蛇
  6. html高德地图ip定位之后拖动自定义定位地
  7. knitr html输出中的字符串太长
  8. 将div放在float下面:left divs
  9. 如何提交位于内的输入?
  10. (android 实战总结)android对html支持接