Guest User

Untitled

a guest
Jan 18th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Created by PhpStorm.
  5. * User: mac
  6. * Date: 2018/1/9
  7. * Time: 21:14
  8. */
  9. class Upload
  10. {
  11. protected $name;
  12. protected $allow_size;
  13. protected $allow_mime;
  14. protected $allow_ext;
  15. protected $upload;
  16. protected $upfile;
  17. protected $error = null;
  18. protected $extension;
  19. protected $newName;
  20.  
  21. public function __construct(
  22. $upload = "./uploads",
  23. $name = 'pic',
  24. $allow_size = 3000000,
  25. $allow_mime = array('image/jpeg', 'image/gif'),
  26. $allow_ext = array('jpg', 'gif', 'png')
  27. )
  28. {
  29. $this->upload = $upload;
  30. $this->name = $name;
  31. $this->allow_size = $allow_size;
  32. $this->allow_mime = $allow_mime;
  33. $this->allow_ext = $allow_ext;
  34.  
  35. //1,观察数组
  36. $this->upfile = $_FILES[$this->name];
  37. //name--type--temp_name--error--size
  38. }
  39.  
  40. //2,判断错误
  41.  
  42. public function upFile()
  43. {
  44. //观察数组,在构造方法中首先执行
  45.  
  46. //判断错误
  47. $this->judgeError();
  48. //判断大小
  49. $this->judgeSize();
  50. //判断mime
  51. $this->judgeMime();
  52. //判断ext
  53. $this->judgeExt();
  54.  
  55. if (is_null($this->error)) {
  56. //新建目录,文件名
  57. $this->createDir();
  58. //8,移动文件
  59. if (is_uploaded_file($this->upfile['tmp_name'])) {
  60. $res = move_uploaded_file($this->upfile['tmp_name'], $this->upload . '/' . $this->newName);
  61. if ($res) {
  62. $info = array(
  63. 'name' => $this->upfile['name'],
  64. 'newname' => $this->newName,
  65. 'ext' => $this->extension,
  66. 'mime' => $this->upfile['type'],
  67. 'size' => $this->upfile['size']
  68. );
  69. return $info;
  70. }
  71. }
  72. }
  73.  
  74. }
  75.  
  76. //3,判断大小
  77.  
  78. protected function judgeError()
  79. {
  80. if ($this->upfile['error'] > 0) {
  81. switch ($this->upfile['error']) {
  82. case 1:
  83. $this->error = '超出php upload_max_filesize大小';
  84. return false;
  85. case 2:
  86. $this->error = '超出html MAX_FILE_SIZE大小';
  87. return false;
  88. case 3:
  89. $this->error = '只有部分文件';
  90. return false;
  91. case 4:
  92. $this->error = '没有文件上传';
  93. return false;
  94. case 6:
  95. $this->error = '找不到临时文件夹';
  96. return false;
  97. case 7:
  98. $this->error = '临时文件夹不可写';
  99. return false;
  100. }
  101. }
  102. }
  103.  
  104. //4,判断mime类型
  105.  
  106. protected function judgeSize()
  107. {
  108. if ($this->upfile['size'] > $this->allow_size) {
  109. $this->error = '文件大小超出了指定范围';
  110. return false;
  111. }
  112. }
  113.  
  114. //5,判断扩展名
  115.  
  116. protected function judgeMime()
  117. {
  118. if (!in_array($this->upfile['type'], $this->allow_mime)) {
  119. $this->error = 'mime类型不被允许';
  120. return false;
  121. }
  122. }
  123.  
  124. protected function judgeExt()
  125. {
  126. $this->extension = pathinfo($this->upfile['name'], PATHINFO_EXTENSION);
  127. if (!in_array($this->extension, $this->allow_ext)) {
  128. $this->error = '扩展名不允许';
  129. return false;
  130. }
  131. }
  132.  
  133. protected function createDir()
  134. {
  135. //6,新建目录
  136.  
  137. if (!file_exists($this->upload)) {
  138. mkdir($this->upload, 0755);
  139. }
  140. //7,准备随机文件名
  141. $this->newName = md5(time(), mt_rand()) . '.' . $this->extension;
  142. }
  143.  
  144. public function getError()
  145. {
  146. return $this->error;
  147. }
  148. }
Add Comment
Please, Sign In to add comment