自定义封装上传类

2018-06-28 01:56:21   php分享记录

 

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: john shuxian
  5. * Date: 2018/6/22
  6. * Time: 16:58
  7. */
  8. error_reporting(E_ALL);
  9. class uploadsController
  10. {
  11. protected $ext;#如png,jpg
  12. protected $max_file_size;
  13. protected $save_path;
  14. protected $error_msg = array();
  15. protected $files;#表单name
  16. protected $realname;#文件真实名
  17. protected $type;#文件类型 image flash
  18. protected $allow_ext = array();
  19. public function __construct($files)
  20. {
  21. //定义允许上传的文件扩展名
  22. $this->allow_ext = array(
  23. 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
  24. 'flash' => array('swf', 'flv'),
  25. 'media' => array('swf', 'flv', 'mp3', 'wav', 'wm a', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
  26. 'file' => array('doc', 'docx', 'xls', 'xlsx', 'pp t', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','sql'),
  27. );
  28. $this->max_file_size = 90*1024*1024;
  29. $this->save_path = "./uploads/";
  30. $this->files = $files;
  31. }
  32. public function uploads()
  33. {
  34. if($this->checkError($_FILES[$this->files])){
  35. if(!file_exists($this->save_path)){
  36. //文件夹不存在时,自动创建目录
  37. mkdir($this->save_path);
  38. }
  39. $new_name = $this->randomName($this->ext);
  40. if(move_uploaded_file($_FILES[$this->files]['tmp_name'],$new_name) === false){
  41. array_push($this->error_msg,"文件上传失败,请重试");
  42. $this->dd($this->error_msg);
  43. }else{
  44. return json_encode(array('type'=>"success",'new_file'=>$new_name));
  45. }
  46. }
  47. }
  48. /**
  49. * 返回随机文件名
  50. * @param $ext
  51. * @return string
  52. */
  53. public function randomName($ext){
  54. return $this->save_path.date("YmdHis")."_".rand(1000,9000).".".$ext;
  55. }
  56. /**
  57. * 检查错误
  58. * @param $files_post
  59. * @return bool
  60. */
  61. public function checkError($files_post)
  62. {
  63. if(empty($files_post)){
  64. array_push($this->error_msg,"未知错误");
  65. $this->dd($this->error_msg,false);
  66. return false;
  67. }else{
  68. $error = $files_post['error'];
  69. if($error!=0){
  70. switch ($error){
  71. case 1:
  72. array_push($this->error_msg,"上传文件超过系统设置");
  73. break;
  74. case 2:
  75. array_push($this->error_msg,"上传的文件大小超过了HTML表单设置的MAX_FILE_SIZE选项设置的值");
  76. break;
  77. case 3:
  78. array_push($this->error_msg,"文件只有部分被上传");
  79. break;
  80. case 4:
  81. array_push($this->error_msg,"没有文件被上传");
  82. break;
  83. case 6:
  84. array_push($this->error_msg,"找不到临时文件夹");
  85. break;
  86. case 7:
  87. array_push($this->error_msg,"文件写入失败");
  88. break;
  89. }
  90. return false;
  91. }else{
  92. $this->realname = $files_post['name'];
  93. $this->ext =explode('.',$this->realname)[1];
  94. $this->type = explode('/',$files_post['type'])[0];
  95. if(isset($this->allow_ext[$this->type])&&in_array($this->ext,$this->allow_ext[$this->type])){
  96. if($files_post['size']>$this->max_file_size){
  97. array_push($this->error_msg,"文件大小超出限制,请选择小于".$this->max_file_size/(1024*1024)."M的文件");
  98. $this->dd($this->error_msg);
  99. }
  100. return true;
  101. }else{
  102. array_push($this->error_msg,"文件类型不支持");
  103. $this->dd($this->error_msg);
  104. return false;
  105. }
  106. }
  107. }
  108. }
  109. public function dd(array $values,$echo = true){
  110. $log_name = "./".date("Y-m-d").".log";
  111. if(!$echo){
  112. $file = fopen($log_name,'w+');
  113. if(!is_writable($log_name)){
  114. array_push($this->error_msg,"日志文件不可写,请检查文件权限");
  115. var_dump($this->error_msg);
  116. die();
  117. }
  118. foreach ($values as $v){
  119. $vv = $v;
  120. if(!is_string($v) && !is_numeric($v)){
  121. $vv = json_encode($v);
  122. }
  123. $vv.=PHP_EOL;
  124. fwrite($file,$vv);
  125. }
  126. fclose($file);
  127. }else{
  128. var_dump($values);
  129. die();
  130. }
  131. }
  132. }
  133. $uploads = new uploadsController('pic');
  134. $uploads->uploads();