Upload.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 文件管理-分层控制器
  7. */
  8. namespace app\common\widget;
  9. use think\facade\Env;
  10. use think\facade\Config;
  11. use upload\Upload as upDriver;
  12. class Upload {
  13. protected $rootpath;
  14. protected $config;
  15. public function __construct() {
  16. $this->config = config::get('upload.');
  17. }
  18. /**
  19. * 文件上传
  20. */
  21. public function index($dir_path = ''){
  22. $uploadDriver = $this->config['upload_driver_config'];
  23. $upConfig = array(
  24. 'maxSize' => intval($this->config['upload_size']) * 1024 * 1024,
  25. 'allowExts' => explode(',',$this->config['upload_exts']),
  26. 'rootPath' => PATH_RES,
  27. 'savePath' => $dir_path ? $dir_path.DS.$this->config['upload_path']: $this->config['upload_path'],
  28. 'saveRule' => 'md5_file',
  29. 'driver' => $this->config['upload_driver'],
  30. 'driverConfig' => $uploadDriver[$this->config['upload_driver']],
  31. );
  32. //上传
  33. $upload = new upDriver($upConfig);
  34. if (!$upload->upload()) {
  35. return ['error'=>1,'message' => $upload->getError()];
  36. }
  37. //上传信息
  38. $list = $upload->getUploadFileInfo();
  39. if (empty($list)) {
  40. return ['error'=>1,'message'=>'上传文件不存在'];
  41. }
  42. if($this->config['upload_driver'] == 'oss'){
  43. foreach ($list as $info) {
  44. $filepath = $info['url'];
  45. }
  46. }else{
  47. foreach ($list as $key => $info) {
  48. $imgpath = $info['savepath'].$info['savename'];
  49. $filepath = str_replace('\\','/',substr($imgpath,strlen(PATH_PUBLIC)));
  50. $filepath = empty($this->config['upload_relative']) ? '/'.$filepath : $this->config['upload_relative'].$filepath;
  51. }
  52. }
  53. return ['error'=>0,'message'=>'成功','url' => $filepath];
  54. }
  55. /**
  56. * 文件证书文件cart到runtime目录(外网没办法直接访问)
  57. */
  58. public function cert($dir_path = 0){
  59. $uploadDriver = $this->config['upload_driver_config'];
  60. $upConfig = array(
  61. 'maxSize' => intval($this->config['upload_size']) * 1024 * 1024,
  62. 'allowExts' => ['pem'],
  63. 'rootPath' => Env::get('runtime_path'),
  64. 'savePath' => 'cert'.DS.$dir_path.DS,
  65. 'saveRule' => 'md5_file',
  66. 'driver' => 'local',
  67. 'driverConfig' => $uploadDriver['local'],
  68. );
  69. //上传
  70. $upload = new upDriver($upConfig);
  71. if (!$upload->upload()) {
  72. return ['error'=>1,'message'=>$upload->getError()];
  73. }
  74. //上传信息
  75. $rel = $upload->getUploadFileInfo();
  76. if (empty($rel)) {
  77. return ['error'=>1,'message'=>'上传文件不存在'];
  78. }
  79. return ['error'=>0,'message'=>'成功','url' => $rel['file']['savename']];
  80. }
  81. /**
  82. * 读取目录文件
  83. * @param [string] $path [默认访问目录]
  84. * @param [string] $is_tpl [是否模板目录]
  85. * @return [string] $dir_path [模仿根目录]
  86. */
  87. public function directoryResource($path = null,$is_tpl = false,$dir_path = ''){
  88. $folder = $is_tpl ? PATH_THEMES: PATH_RES;
  89. $root_path = $dir_path ? $folder.$dir_path.DS : $folder;
  90. $folder_path = self::accessPath($path,$root_path);
  91. if(!self::isDir($folder_path['nowpath'])) return; //没有权限直接返回
  92. $file_list['backpath'] = [];
  93. $file_list['folder'] = [];
  94. $file_list['file'] = [];
  95. if(isset($folder_path['backpath'])){
  96. $file_list['backpath'][0]['name'] = '返回上级';
  97. $file_list['backpath'][0]['path'] = $folder_path['backpath'];
  98. }
  99. $filepath = str_replace('\\','/',substr($folder_path['nowpath'],strlen(PATH_PUBLIC)));
  100. $imgurl = $filepath ? $filepath:'/';
  101. $file_info = scandir($folder_path['nowpath']);
  102. $relative = empty($this->config['upload_relative']) ? '/' : $this->config['upload_relative'];
  103. foreach ($file_info as $key => $value) {
  104. if(is_dir($folder_path['nowpath'].'/'.$value)){
  105. if ($value != "." && $value != ".."){
  106. $file_list['folder'][] = ['name' => $value,'path' =>$path.'/'.$value];
  107. }
  108. }else{
  109. $file_list['file'][] = [$relative.$imgurl.$value,$value];
  110. }
  111. }
  112. krsort($file_list['file']);
  113. krsort($file_list['folder']);
  114. return $file_list;
  115. }
  116. /**
  117. * 处理访问路径参数
  118. * @param [type] $path [当前访问路径]
  119. * @return array [上一级目录或当前目录]
  120. */
  121. private function accessPath($path = null,$root_path = PATH_RES){
  122. if($path){
  123. $path = str_replace('\\','/',$path);
  124. $path = str_replace('../','',$path);
  125. $path = str_replace('..','',$path);
  126. $newpath = realpath($root_path.$path);
  127. if(is_dir($newpath)){
  128. $len = strlen($root_path);
  129. $back_path = substr($newpath,$len);
  130. $folder_ary = $back_path ? explode('\\',$back_path) : [];
  131. array_pop($folder_ary);
  132. $folder_path['backpath'] = implode('/',$folder_ary);
  133. $folder_path['nowpath'] = $newpath.DS;
  134. }else{
  135. $folder_path['nowpath'] = $root_path;
  136. }
  137. return $folder_path;
  138. }
  139. $folder_path['nowpath'] = $root_path;
  140. return $folder_path;
  141. }
  142. /**
  143. * 检测目录权限
  144. * @param [type] $path [当前判断目录]
  145. * @return [boolean] [是否存在或有权限]
  146. */
  147. private function isDir($path) {
  148. if(!(is_dir($path) && is_writable($path))){
  149. $this->errorMsg = '上传根目录不存在!';
  150. return false;
  151. }
  152. return true;
  153. }
  154. }