FileManager.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * upload文件浏览器
  4. *
  5. * @version 0.0.0
  6. * @author by huwhois
  7. * @time 2017/11/27
  8. */
  9. namespace app\admin\controller;
  10. use think\App;
  11. use think\Request;
  12. use think\facade\View;
  13. use think\Image;
  14. class FileManager extends Base
  15. {
  16. protected $img_path = '';
  17. protected $t_suffix = '_thumb';
  18. protected $t_width = 400;
  19. protected $t_height = 300;
  20. protected $request;
  21. public function uploadimg($thumb=false, $width=400, $height=300)
  22. {
  23. if ($this->request->isPost()) {
  24. $file = $this->request->file('upload_file');
  25. if ($file) {
  26. try {
  27. validate(
  28. [
  29. 'file' => [
  30. // 限制文件大小(单位b),这里限制为20M
  31. 'fileSize' => 20 * 1024 * 1024,
  32. // 限制文件后缀,多个后缀以英文逗号分割
  33. 'fileExt' => 'jpg,png,gif,jpeg,webp'
  34. ]
  35. ],
  36. [
  37. 'file.fileSize' => '文件太大',
  38. 'file.fileExt' => '不支持的文件后缀',
  39. ]
  40. )->check(['file' => $file]);
  41. $savename = \think\facade\Filesystem::disk('public')->putFile( '/', $file);
  42. $thumbname = "";
  43. if ($thumb) {
  44. $thumbname = $this->makeThumb($savename, $width, $height);
  45. }
  46. } catch (\think\exception\ValidateException $e) {
  47. $this->error($e->getMessage());
  48. }
  49. unset($file);
  50. return [
  51. 'code'=>2,
  52. 'picname' => '/storage/' . str_replace('\\', '/', $savename),
  53. 'thumbname'=>'/storage/' . $thumbname
  54. ];
  55. } else {
  56. $this->error('图片不能为空');
  57. }
  58. } else {
  59. View::assign('thumb', $thumb);
  60. View::assign('width', $width);
  61. View::assign('height', $height);
  62. return View::fetch();
  63. }
  64. }
  65. /**
  66. * 生成缩略图
  67. * @param string $filename 必须参数, 图片路径名(相对or绝对)(/public/uploads/...)
  68. * @param int $width 缩略图宽值, 默认 384px;
  69. * @param int $height 缩略图高值, 默认 224px;
  70. * @param int $type 缩略图裁剪方式, 默认值 1, 固定尺寸缩放; 其他: 1, 等比例缩放;
  71. * 2, 缩放后填充; 3, 居中裁剪; 4, 左上角裁剪; 5, 右下角裁剪
  72. * @return string $thumbname 缩略图文件名
  73. */
  74. public function makeThumb($filename, $width =384, $height = 224, $type = 1)
  75. {
  76. $file ='./storage/' . str_replace('\\', '/', $filename);
  77. $ext = pathinfo($file, PATHINFO_EXTENSION);
  78. $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $filename)) . $this->t_suffix . '.' . $ext;
  79. $image = Image::open($file);
  80. $result = $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);
  81. return $result ? $thumbname : '';
  82. }
  83. public function index()
  84. {
  85. $data = $this->explorer();
  86. View::assign('dirs', $data['dirs']);
  87. View::assign('files', $data['files']);
  88. View::assign('counts', $data['counts']);
  89. View::assign('activeurl', $data['activeurl']);
  90. View::assign('activepath', $data['activepath']);
  91. return View::fetch();
  92. }
  93. /**
  94. * 获取当前文件相关信息
  95. * @return array $data 文件信息数据组
  96. */
  97. protected function explorer()
  98. {
  99. $param = $this->request->param();
  100. $activepath = isset($param['activepath'])? $param['activepath'] : '';
  101. $inpath = "";
  102. $inpath = $this->img_path . $activepath;
  103. $dirhandle = scandir($inpath);
  104. $dirs = $files = [];
  105. define('BKM', 1024);
  106. foreach ($dirhandle as $val) {
  107. if ($val == "." || $val == "..") {
  108. continue;
  109. } elseif (is_dir($inpath . DIRECTORY_SEPARATOR . $val)) {
  110. $dirs[] = $val;
  111. } else {
  112. $arr = [];
  113. $file = '';
  114. $file = $inpath . DIRECTORY_SEPARATOR . $val;
  115. $arr['name'] = $val;
  116. $arr['extension'] = pathinfo($file, PATHINFO_EXTENSION);
  117. $filesize = floatval(filesize($file));
  118. if ($filesize>BKM) {
  119. $filesize = round($filesize/BKM, 2);
  120. if ($filesize>BKM) {
  121. $filesize = round($filesize/BKM, 2);
  122. $filesize .="M";
  123. } else {
  124. $filesize .="K";
  125. }
  126. } else {
  127. $filesize .="B";
  128. }
  129. $arr['size'] = $filesize;
  130. $filetime = filemtime("$file");
  131. $arr['time'] = date("Y-m-d H:i:s", $filetime);
  132. $files[] = $arr;
  133. }
  134. }
  135. $counts = count($dirs)+count($files);
  136. $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);
  137. $data = [
  138. 'dirs' => $dirs,
  139. 'files' => $files,
  140. 'counts' => $counts,
  141. 'activeurl' => $activeurl,
  142. 'activepath'=> $activepath,
  143. ];
  144. return $data;
  145. }
  146. public function delDir()
  147. {
  148. if ($this->request->isAjax()) {
  149. $activepath = $this->request->param('activepath');
  150. $activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);
  151. $dir = $this->request->param('dir');
  152. $dir_name = app()->getRootPath.'public'.DIRECTORY_SEPARATOR.$activepath.DIRECTORY_SEPARATOR.$dir;
  153. if (count(scandir($dir_name)) > 2) {
  154. return ['status'=>1,'msg'=>'不可删除非空目录'];
  155. }
  156. if (rmdir($dir_name)) {
  157. return ['status'=>2,'msg'=>'success'];
  158. } else {
  159. return ['status'=>0,'msg'=>'failed'];
  160. }
  161. }
  162. }
  163. public function del()
  164. {
  165. if ($this->request->isAjax()) {
  166. $activepath = $this->request->param('activepath');
  167. $activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);
  168. $filename = $this->request->param('filename');
  169. if (unlink(app()->getRootPath.'public'.DIRECTORY_SEPARATOR.$activepath.DIRECTORY_SEPARATOR.$filename)) {
  170. return ['status'=>1,'msg'=>'success'];
  171. } else {
  172. return ['status'=>0,'msg'=>'failed'];
  173. }
  174. }
  175. }
  176. // public function upload()
  177. // {
  178. // if ($this->request->isGet()) {
  179. // return View::fetch()();
  180. // } else {
  181. // $isthumb = $this->request->has('isthumb', 'post') ? $this->request->post('isthumb') : 0;
  182. // $mode = $this->request->has('formername', 'post') ? $this->request->post('formername') : 0;
  183. // $file = request()->file('image');
  184. // $info = $this->saveUpload($file, $mode);
  185. // if (!$info) {
  186. // $this->error($info->getError());
  187. // }
  188. // $filename = $info->getSaveName();
  189. // unset($info);
  190. // if ($isthumb == 1) {
  191. // $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;
  192. // $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;
  193. // $thumbname = $this->makeThumb($filename, $width, $height);
  194. // if (!$thumbname) {
  195. // $this->error('缩略图生成失败');
  196. // }
  197. // } else {
  198. // $thumbname = '';
  199. // }
  200. // if ($this->request->isAjax()) {
  201. // return ['code'=>2, 'picname' => $filename, 'thumbname'=>$thumbname];
  202. // } else {
  203. // $this->success('上传成功', '/file_manager/index');
  204. // }
  205. // }
  206. // }
  207. /**
  208. * 保存上传的图片
  209. * @param object $file 获取的上传对象
  210. * @param boolean $mode 是否保存原文件名 0(false), 生成新名称; 1(true), 保留原名
  211. * @return object 返回文件保存对象
  212. */
  213. protected function saveUpload($file, $mode)
  214. {
  215. $validate = ['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'];
  216. if ($mode) {
  217. $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads', '');
  218. } else {
  219. $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads');
  220. }
  221. return $upload;
  222. }
  223. /**
  224. * 站内选择
  225. * @return void
  226. */
  227. public function selectPicture()
  228. {
  229. $data = $this->explorer();
  230. View::assign('data', $data);
  231. return View::fetch()();
  232. }
  233. public function uploadPicture()
  234. {
  235. return View::fetch()();
  236. }
  237. // public function onlinePicture()
  238. // {
  239. // if ($this->request->isAjax()) {
  240. // $param = $this->request->param();
  241. // $urlimg = $param['filename'];
  242. // $download = $param['download'];
  243. // $isthumb = $param['isthumb'];
  244. // $formername = $param['formername'];
  245. // $res = \mylib\GetImageByurl::isImg($urlimg);
  246. // if (!$res) {
  247. // $this->error('该图片不合法');
  248. // } else {
  249. // if (!$download) {
  250. // return ['code'=>2,'filename'=>$urlimg];
  251. // } else {
  252. // $basepath = \config('url_domain_root') . '/uploads';
  253. // // var_dump($param);
  254. // if ($isthumb!=1 && preg_match("#" . $basepath . "#i", $urlimg)) {
  255. // $this->error('图片已在服务其中, 可直接选用');
  256. // }
  257. // // 按文件夹日期夹存放图片
  258. // $today = date('Ymd', time());
  259. // $savePath = $this->img_path . $today;
  260. // if (!is_dir($savePath)) {
  261. // if (mkdir($savePath) == false) {
  262. // $this->error('下载失败, 请稍后重试');
  263. // }
  264. // }
  265. // if ($formername==1) {
  266. // // 获取原文件名
  267. // $fileinfo = pathinfo($urlimg);
  268. // if (!isset($fileinfo['extension']) || !in_array($fileinfo['extension'], ['jpg', 'jpeg', 'png', 'gif'])) {
  269. // $ext = \mylib\GetImageByurl::getEXTENSION($urlimg);
  270. // }
  271. // $filename = $fileinfo['basename'] . '.' . $ext;
  272. // } else {
  273. // $filename = '';
  274. // }
  275. // $filename = \mylib\GetImageByurl::getImageByurl($urlimg, $filename, $savePath);
  276. // if ($filename) {
  277. // // 生成缩略图
  278. // if ($isthumb==1) {
  279. // $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;
  280. // $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;
  281. // $thumbname = $this->makeThumb($today . DIRECTORY_SEPARATOR . $filename, $width, $height);
  282. // if (!$thumbname) {
  283. // $this->error('缩略图生成失败');
  284. // }
  285. // } else {
  286. // $thumbname = '';
  287. // }
  288. // return ['code'=>2, 'filename' => $today . DIRECTORY_SEPARATOR . $filename, 'thumbname'=>$thumbname];
  289. // } else {
  290. // $this->error('图片下载失败');
  291. // }
  292. // }
  293. // }
  294. // } else {
  295. // return View::fetch()();
  296. // }
  297. // }
  298. /**
  299. * 判断(远程)文件是否为图片
  300. */
  301. // public function isImg()
  302. // {
  303. // if ($this->request->isAjax()) {
  304. // $filename = $this->request->param('filename');
  305. // $res = \mylib\GetImageByurl::isImg($filename);
  306. // if (!$res) {
  307. // $this->error('该图片不合法');
  308. // } else {
  309. // return ['code'=>2,'msg'=>'图片地址可用'];
  310. // }
  311. // }
  312. // }
  313. }