FileManager.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. <<<<<<< HEAD
  2. <?php
  3. declare(strict_types=1);
  4. /**
  5. * upload文件管理
  6. *
  7. * @version 0.0.0
  8. * @author by huwhois
  9. * @time 2017/11/27
  10. */
  11. namespace app\sys\controller;
  12. use Exception;
  13. use UnexpectedValueException;
  14. use DirectoryIterator;
  15. use think\facade\View;
  16. use think\File;
  17. use think\Image;
  18. use think\facade\Config;
  19. use think\exception\ValidateException;
  20. use app\common\service\FileService;
  21. use app\common\model\FileManager as FileManagerModel;
  22. use think\file\UploadedFile;
  23. class FileManager extends Base
  24. {
  25. protected $modelName = 'FileManager';
  26. protected $t_suffix = '_thumb';
  27. protected $width = 400; // 缩略图高度
  28. protected $height = 300;
  29. protected $storage_path = 'public/storage';
  30. public function index()
  31. {
  32. $param = $this->request->param();
  33. $param['limit'] = isset($param['limit']) ? (int) $param['limit'] : Config::get('app.page_size');
  34. $list = FileManagerModel::queryPage($param);
  35. View::assign('list', $list);
  36. return View::fetch();
  37. }
  38. /**
  39. * 浏览文件
  40. */
  41. public function explorer()
  42. {
  43. $param = $this->request->param();
  44. $activepath = isset($param['activepath']) ? $param['activepath'] : '';
  45. $realpath = $this->app->getRootPath() . $this->storage_path . $activepath;
  46. try {
  47. $dirhandle = new DirectoryIterator($realpath);
  48. $dirs = $files = [];
  49. foreach ($dirhandle as $fileInfo) {
  50. if($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
  51. continue;
  52. } elseif ($fileInfo->isDir()) {
  53. $dirs[] = $fileInfo->getFilename();
  54. } elseif ($fileInfo->isFile()) {
  55. $files[] = [
  56. 'filename' => $fileInfo->getFilename(),
  57. 'extension' => strtolower($fileInfo->getExtension()),
  58. 'size' => format_bytes($fileInfo->getSize()),
  59. 'time' => $fileInfo->getCTime(),
  60. ];
  61. }
  62. }
  63. $counts = count($dirs) + count($files);
  64. $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);
  65. View::assign([
  66. 'dirs' => $dirs,
  67. 'files' => $files,
  68. 'counts' => $counts,
  69. 'activeurl' => $activeurl,
  70. 'activepath' => $activepath,
  71. ]);
  72. return View::fetch();
  73. } catch(UnexpectedValueException $uve) {
  74. $this->error($uve->getMessage());
  75. }
  76. }
  77. /**
  78. * 附件上传
  79. */
  80. public function uploadFile()
  81. {
  82. $activepath = $this->request->has('activepath') ? $this->request->param('activepath') : '';
  83. $files = $this->request->file('upload_file');
  84. if ($files) {
  85. try {
  86. validate(
  87. [
  88. 'file' => [
  89. // 限制文件大小(单位b),这里限制为10M
  90. 'fileSize' => 10 * 1024 * 1024,
  91. // 限制文件后缀,多个后缀以英文逗号分割
  92. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif,pdf,doc,docx,xls,xlsx,ppt,pptx,txt'
  93. ]
  94. ],
  95. [
  96. 'file.fileSize' => '文件太大',
  97. 'file.fileExt' => '不支持的文件后缀',
  98. ]
  99. )->check(['file' => $files]);
  100. $savenames = [];
  101. $uploadFiles = [];
  102. if (!is_array($files) && $files instanceof UploadedFile) {
  103. $uploadFiles[] = $files;
  104. } else {
  105. $uploadFiles = $files;
  106. }
  107. $url = Config::get('filesystem.disks.public.url');
  108. foreach ($uploadFiles as $file) {
  109. $fileinfo = new FileManagerModel();
  110. $fileinfo->title = $file->getOriginalName();
  111. $fileinfo->filesize = $file->getSize();
  112. $fileinfo->filetime = $file->getCTime();
  113. $fileinfo->fileextension = $file->extension();
  114. $fileinfo->username = $this->getSysUser()->username;
  115. $fileinfo->hash_md5 = $file->md5();
  116. if (empty($activepath)) {
  117. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  118. $fileinfo->filepath = $url . '/' . $savename;
  119. $fileinfo->filename = basename($savename);
  120. } else {
  121. $savename = $activepath . '/' . $file->hashName();
  122. $savepath = $this->app->getRootPath() . $this->storage_path . $savename;
  123. $file = $file->move($this->app->getRootPath() . $this->storage_path . $activepath, $savepath);
  124. $fileinfo->filepath = $url . $savename;
  125. $fileinfo->filename = $file->hashName();
  126. }
  127. $fileinfo->save();
  128. $savenames[] = $fileinfo->filepath;
  129. }
  130. return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
  131. } catch (ValidateException $e) {
  132. $this->error($e->getMessage());
  133. }
  134. } else {
  135. $this->error('图片不能为空');
  136. }
  137. }
  138. /**
  139. * 修改文件title
  140. */
  141. public function updateFileTitle($id = 0, $filetitle = '')
  142. {
  143. $fileInfo = FileManagerModel::find($id);
  144. if ($fileInfo == null) {
  145. return json(['code'=>1, 'fileid='.$id.'不存在']);
  146. }
  147. $fileInfo->title = $filetitle;
  148. if ($fileInfo->save()) {
  149. return json(['code'=>0, 'msg'=>'success']);
  150. } else {
  151. return json(['code'=>1, 'msg'=>'failed']);
  152. }
  153. }
  154. /**
  155. * 删除
  156. * @param int|array $id info id
  157. * @return array
  158. */
  159. public function delete($id)
  160. {
  161. if ($this->request->isPost()) {
  162. $whereOp = '=';
  163. if (is_array($id)) {
  164. $whereOp = 'IN';
  165. }
  166. $list = FileManagerModel::where('fileid', $whereOp, $id)->select();
  167. foreach ($list as $value) {
  168. $file = $this->app->getRootPath() . 'public' . $value->filepath;
  169. if (file_exists($file)) {
  170. unlink($file);
  171. }
  172. }
  173. if (FileManagerModel::destroy($id)) {
  174. return ['code' => 0,'msg'=>'删除成功'];
  175. } else {
  176. return ['code' => 1,'msg'=>'删除失败'];
  177. }
  178. }
  179. }
  180. /**
  181. * 删除空目录
  182. */
  183. public function deldir()
  184. {
  185. if ($this->request->isPost()) {
  186. $dir = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('dir'));
  187. $dir_name = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $dir;
  188. try {
  189. rmdir($dir_name);
  190. return ['code' => 0, 'msg' => 'success'];
  191. } catch (Exception $e) {
  192. return ['code' => 1, 'msg' => 'failed, 目录不为空'];
  193. }
  194. }
  195. }
  196. /**
  197. * 删除文件
  198. */
  199. public function delfile()
  200. {
  201. if ($this->request->isPost()) {
  202. $filename = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('filename'));
  203. $filepath = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $filename;
  204. if (unlink($filepath)) {
  205. return ['code' => 0, 'msg' => 'success'];
  206. } else {
  207. return ['code' => 1, 'msg' => 'failed'];
  208. }
  209. }
  210. }
  211. /**
  212. * 处理上传的图片
  213. */
  214. protected function dealUploadImg(File $file, $water, $thumb, $width, $height, $overwrite)
  215. {
  216. $savename = "";
  217. $thumbname = "";
  218. if ($water == true || $thumb == true) {
  219. $image = Image::open($file);
  220. if ($water) {
  221. $type = $this->system->water_type ?: Config::get('filesystem.water.type');
  222. if ($type == 'water') {
  223. $watemark = $this->system->watermark ?: Config::get('filesystem.water.watermark');
  224. $image->water($watemark);
  225. } else {
  226. $watetext = $this->system->watertext ?: Config::get('filesystem.water.watertext');
  227. $image->text($watetext, Config::get('filesystem.water.waterfont'), 30, '#ffffff30');
  228. }
  229. }
  230. $savename = $file->hashName();
  231. $realPath = Config::get('filesystem.disks.public.root') . '/' . $savename;
  232. if ($thumb == true) {
  233. if ($overwrite == true) {
  234. $image->thumb($width, $height, 1);
  235. $image->save($realPath);
  236. } else {
  237. $image->save($realPath);
  238. $image->thumb($width, $height, 1);
  239. $ext = $file->extension();
  240. $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $savename)) . $this->t_suffix . '.' . $ext;
  241. $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);
  242. }
  243. } else {
  244. $image->save($realPath);
  245. }
  246. unset($image);
  247. } else {
  248. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  249. }
  250. return [
  251. 'picname' => str_replace('\\', '/', $savename),
  252. 'thumbname' => $thumbname
  253. ];
  254. }
  255. /**
  256. * 图片上传(iframe 页面)
  257. */
  258. public function uploadimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
  259. {
  260. View::assign([
  261. 'img_id' => $img_id,
  262. 'water' => $water,
  263. 'thumb' => $thumb,
  264. 'width' => $width,
  265. 'height' => $height,
  266. 'overwrite' => $overwrite
  267. ]);
  268. return View::fetch();
  269. }
  270. /**
  271. * 本地图片上传
  272. * @file upload_file 上传的文件
  273. * @param string $img_id 图片ipnut text id 默认值 picture
  274. * @param boolean $water 是否添加水印
  275. * @param boolean $thumb 是否制作缩略图
  276. * @param int $width 缩略图最大宽
  277. * @param int $height 缩略图最大高
  278. * @param bool $overwrite 生成缩略图后是否保存原图
  279. */
  280. public function uploadLocalImg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
  281. {
  282. if ($this->request->isPost()) {
  283. $file = $this->request->file('upload_file');
  284. if ($file) {
  285. try {
  286. validate(
  287. [
  288. 'file' => [
  289. // 限制文件大小(单位b),这里限制为4M
  290. 'fileSize' => 4 * 1024 * 1024,
  291. // 限制文件后缀,多个后缀以英文逗号分割
  292. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  293. ]
  294. ],
  295. [
  296. 'file.fileSize' => '文件太大',
  297. 'file.fileExt' => '不支持的文件后缀',
  298. ]
  299. )->check(['file' => $file]);
  300. $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);
  301. return array_merge(
  302. [
  303. 'code' => 0,
  304. 'img_id' => $img_id,
  305. 'picture_url' => Config::get('filesystem.disks.public.url') . '/'
  306. ],
  307. $arr
  308. );
  309. } catch (ValidateException $e) {
  310. $this->error($e->getMessage());
  311. }
  312. } else {
  313. $this->error('图片不能为空');
  314. }
  315. }
  316. }
  317. /**
  318. * 网络图片上传
  319. * @paran string url_file 网络图片地址
  320. * @param string $img_id 图片ipnut text id 默认值 picture
  321. * @param boolean $water 是否添加水印
  322. * @param boolean $thumb 是否制作缩略图
  323. * @param int $width 缩略图最大宽
  324. * @param int $height 缩略图最大高
  325. * @param bool $overwrite 生成缩略图后是否保存原图
  326. */
  327. public function uploadUrlImg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
  328. {
  329. if ($this->request->isPost()) {
  330. $urlImg = $this->request->param('url_file');
  331. if ($urlImg) {
  332. try {
  333. $fileService = new FileService();
  334. $file = $fileService->downloadUrlImg($urlImg);
  335. $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);
  336. @unlink($file->realPath);
  337. return array_merge(
  338. [
  339. 'code' => 0,
  340. 'img_id' => $img_id,
  341. 'picture_url' => Config::get('filesystem.disks.public.url') . '/'
  342. ],
  343. $arr
  344. );
  345. } catch (\think\exception\ValidateException $e) {
  346. $this->error($e->getMessage());
  347. }
  348. } else {
  349. $this->error('图片地址不能为空');
  350. }
  351. }
  352. }
  353. /**
  354. * 选择服务器图片
  355. * @paran string online_file 服务器图片地址
  356. * @param string $img_id 图片ipnut text id 默认值 picture
  357. * @param boolean $water 是否添加水印
  358. * @param boolean $thumb 是否制作缩略图
  359. * @param int $width 缩略图最大宽
  360. * @param int $height 缩略图最大高
  361. * @param bool $overwrite 生成缩略图后是否保存原图
  362. */
  363. public function uploadonlineimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
  364. {
  365. if ($this->request->isPost()) {
  366. $pathImg = $this->request->param('online_file');
  367. if ($pathImg && file_exists($this->app->getRootPath() . "public" . $pathImg)) {
  368. $picname = $pathImg;
  369. $thumbname = "";
  370. if ($thumb) {
  371. if (stripos($picname, $this->t_suffix)) {
  372. $thumbname = $pathImg;
  373. } else {
  374. try {
  375. $file = new File($this->app->getRootPath() . "public" . $pathImg);
  376. $ext = $file->getExtension();
  377. $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $picname)) . $this->t_suffix . '.' . $ext;
  378. if (!file_exists($thumbname)) {
  379. $image = Image::open($file);
  380. $image->thumb($width, $height, 1);
  381. $image->save($this->app->getRootPath() . "public" . $thumbname);
  382. unset($image);
  383. }
  384. if ($overwrite) {
  385. $picname = $thumbname;
  386. }
  387. } catch (Exception $e) {
  388. $this->error($e->getMessage());
  389. }
  390. }
  391. }
  392. return [
  393. 'code' => 0,
  394. 'img_id' => $img_id,
  395. 'picname' => $picname,
  396. 'thumbname' => $thumbname,
  397. ];
  398. } else {
  399. $this->error('图片地址不存在');
  400. }
  401. }
  402. }
  403. public function onlineimg($page)
  404. {
  405. $files = FileService::getFiles(Config::get('filesystem.disks.public.root'));
  406. if (!count($files)) {
  407. return json_encode(array(
  408. "state" => "no match file",
  409. "list" => array(),
  410. "start" => $page,
  411. "total" => count($files)
  412. ));
  413. }
  414. /* 获取指定范围的列表 */
  415. $page = $page - 1 < 0 ? 0 : (int)$page - 1;
  416. $size = 20;
  417. $start = $page * $size;
  418. $end = $start + $size;
  419. $len = count($files);
  420. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
  421. $list[] = $files[$i];
  422. }
  423. return json([
  424. "code" => 0,
  425. "list" => $list,
  426. "page" => $page + 1,
  427. "total" => count($files)
  428. ]);
  429. }
  430. /**
  431. * 判断(远程)文件是否为图片
  432. */
  433. // public function isImg()
  434. // {
  435. // if ($this->request->isAjax()) {
  436. // $filename = $this->request->param('filename');
  437. // $res = \mylib\GetImageByurl::isImg($filename);
  438. // if (!$res) {
  439. // $this->error('该图片不合法');
  440. // } else {
  441. // return ['code'=>2,'msg'=>'图片地址可用'];
  442. // }
  443. // }
  444. // }
  445. /**
  446. * ckeditor 富文本编辑器上传图片
  447. */
  448. public function ckeditorUploadImage()
  449. {
  450. if ($this->request->isPost()) {
  451. $file = $this->request->file('upload');
  452. if ($file) {
  453. try {
  454. validate(
  455. [
  456. 'file' => [
  457. // 限制文件大小(单位b),这里限制为4M
  458. 'fileSize' => 4 * 1024 * 1024,
  459. // 限制文件后缀,多个后缀以英文逗号分割
  460. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  461. ]
  462. ],
  463. [
  464. 'file.fileSize' => '文件太大',
  465. 'file.fileExt' => '不支持的文件后缀',
  466. ]
  467. )->check(['file' => $file]);
  468. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  469. return json([
  470. 'uploaded' => 1,
  471. 'fileName' => basename($savename),
  472. 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename
  473. ]);
  474. } catch (\think\exception\ValidateException $e) {
  475. $this->error($e->getMessage());
  476. return json([
  477. 'uploaded' => 1,
  478. 'error' => ['message' => $e->getMessage()]
  479. ]);
  480. }
  481. } else {
  482. return json([
  483. 'uploaded' => 1,
  484. 'error' => ['message' => '图片不能为空']
  485. ]);
  486. }
  487. }
  488. }
  489. }
  490. =======
  491. <?php
  492. declare(strict_types=1);
  493. /**
  494. * upload文件管理
  495. *
  496. * @version 0.0.0
  497. * @author by huwhois
  498. * @time 2017/11/27
  499. */
  500. namespace app\sys\controller;
  501. use Exception;
  502. use UnexpectedValueException;
  503. use DirectoryIterator;
  504. use think\facade\View;
  505. use think\facade\Config;
  506. use think\File;
  507. use think\Image;
  508. use think\exception\ValidateException;
  509. use think\file\UploadedFile;
  510. use app\common\service\FileService;
  511. use app\common\model\FileManager as FileManagerModel;
  512. use app\common\facade\FileUtils;
  513. class FileManager extends Base
  514. {
  515. protected $modelName = 'FileManager';
  516. protected $t_suffix = '_thumb';
  517. protected $width = 400; // 缩略图高度
  518. protected $height = 300;
  519. protected $storage_path = 'public/storage';
  520. public function index()
  521. {
  522. $param = $this->request->param();
  523. $param['limit'] = isset($param['limit']) ? (int) $param['limit'] : Config::get('app.page_size');
  524. $list = FileManagerModel::queryPage($param);
  525. View::assign('list', $list);
  526. return View::fetch();
  527. }
  528. public function queryList()
  529. {
  530. $param = $this->request->param();
  531. $list = FileManagerModel::queryList($param);
  532. return json(['code'=>0, 'list'=>$list]);
  533. }
  534. /**
  535. * 浏览文件
  536. */
  537. public function explorer()
  538. {
  539. $param = $this->request->param();
  540. $activepath = isset($param['activepath']) ? $param['activepath'] : '';
  541. $realpath = $this->app->getRootPath() . $this->storage_path . $activepath;
  542. try {
  543. $dirhandle = new DirectoryIterator($realpath);
  544. $dirs = $files = [];
  545. foreach ($dirhandle as $fileInfo) {
  546. if($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
  547. continue;
  548. } elseif ($fileInfo->isDir()) {
  549. $dirs[] = $fileInfo->getFilename();
  550. } elseif ($fileInfo->isFile()) {
  551. $files[] = [
  552. 'filename' => $fileInfo->getFilename(),
  553. 'extension' => strtolower($fileInfo->getExtension()),
  554. 'size' => format_bytes($fileInfo->getSize()),
  555. 'time' => $fileInfo->getCTime(),
  556. ];
  557. }
  558. }
  559. $counts = count($dirs) + count($files);
  560. $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);
  561. // halt($activeurl);
  562. if ($this->request->isAjax()) {
  563. return json([
  564. 'code' => 0,
  565. 'dirs' => $dirs,
  566. 'files' => $files,
  567. 'counts' => $counts,
  568. 'activeurl' => $activeurl,
  569. 'activepath'=> $activepath,
  570. ]);
  571. } else {
  572. View::assign([
  573. 'dirs' => $dirs,
  574. 'files' => $files,
  575. 'counts' => $counts,
  576. 'activeurl' => $activeurl,
  577. 'activepath'=> $activepath,
  578. ]);
  579. return View::fetch();
  580. }
  581. } catch(UnexpectedValueException $uve) {
  582. $this->error($uve->getMessage());
  583. }
  584. }
  585. /**
  586. * 附件上传
  587. */
  588. public function uploadFile()
  589. {
  590. $activepath = $this->request->has('activepath') ? $this->request->param('activepath') : '';
  591. $files = $this->request->file('upload_file');
  592. if ($files) {
  593. try {
  594. validate(
  595. [
  596. 'file' => [
  597. // 限制文件大小(单位b),这里限制为10M
  598. 'fileSize' => 10 * 1024 * 1024,
  599. // 限制文件后缀,多个后缀以英文逗号分割
  600. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif,pdf,doc,docx,xls,xlsx,ppt,pptx,txt'
  601. ]
  602. ],
  603. [
  604. 'file.fileSize' => '文件太大',
  605. 'file.fileExt' => '不支持的文件后缀',
  606. ]
  607. )->check(['file' => $files]);
  608. $savenames = [];
  609. $uploadFiles = [];
  610. if (!is_array($files) && $files instanceof UploadedFile) {
  611. $uploadFiles[] = $files;
  612. } else {
  613. $uploadFiles = $files;
  614. }
  615. $url = Config::get('filesystem.disks.public.url');
  616. foreach ($uploadFiles as $file) {
  617. $fileinfo = new FileManagerModel();
  618. $fileinfo->title = $file->getOriginalName();
  619. $fileinfo->filesize = $file->getSize();
  620. $fileinfo->filetime = $file->getCTime();
  621. $fileinfo->fileextension = $file->extension();
  622. $fileinfo->username = $this->getSysUser()->username;
  623. $fileinfo->hash_md5 = $file->md5();
  624. if (empty($activepath)) {
  625. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  626. $fileinfo->filepath = $url . '/' . $savename;
  627. $fileinfo->filename = basename($savename);
  628. } else {
  629. $savename = $activepath . '/' . $file->hashName();
  630. $savepath = $this->app->getRootPath() . $this->storage_path . $savename;
  631. $file = $file->move($this->app->getRootPath() . $this->storage_path . $activepath, $savepath);
  632. $fileinfo->filepath = $url . $savename;
  633. $fileinfo->filename = $file->hashName();
  634. }
  635. $fileinfo->save();
  636. $savenames[] = $fileinfo->filepath;
  637. }
  638. return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
  639. } catch (ValidateException $e) {
  640. $this->error($e->getMessage());
  641. }
  642. } else {
  643. $this->error('图片不能为空');
  644. }
  645. }
  646. /**
  647. * 修改文件title
  648. */
  649. public function updateFileTitle($id = 0, $filetitle = '')
  650. {
  651. $fileInfo = FileManagerModel::find($id);
  652. if ($fileInfo == null) {
  653. return json(['code'=>1, 'fileid='.$id.'不存在']);
  654. }
  655. $fileInfo->title = $filetitle;
  656. if ($fileInfo->save()) {
  657. return json(['code'=>0, 'msg'=>'success']);
  658. } else {
  659. return json(['code'=>1, 'msg'=>'failed']);
  660. }
  661. }
  662. /**
  663. * 删除
  664. * @param int|array $id info id
  665. * @return array
  666. */
  667. public function delete($id)
  668. {
  669. if ($this->request->isPost()) {
  670. $whereOp = '=';
  671. if (is_array($id)) {
  672. $whereOp = 'IN';
  673. }
  674. $list = FileManagerModel::where('fileid', $whereOp, $id)->select();
  675. foreach ($list as $value) {
  676. $file = $this->app->getRootPath() . 'public' . $value->filepath;
  677. if (file_exists($file)) {
  678. unlink($file);
  679. }
  680. }
  681. if (FileManagerModel::destroy($id)) {
  682. return ['code' => 0,'msg'=>'删除成功'];
  683. } else {
  684. return ['code' => 1,'msg'=>'删除失败'];
  685. }
  686. }
  687. }
  688. /**
  689. * 删除空目录
  690. */
  691. public function deldir()
  692. {
  693. if ($this->request->isPost()) {
  694. $dir = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('dir'));
  695. $dir_name = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $dir;
  696. try {
  697. rmdir($dir_name);
  698. return ['code' => 0, 'msg' => 'success'];
  699. } catch (Exception $e) {
  700. return ['code' => 1, 'msg' => 'failed, 目录不为空'];
  701. }
  702. }
  703. }
  704. /**
  705. * 删除文件
  706. */
  707. public function delfile()
  708. {
  709. if ($this->request->isPost()) {
  710. $filename = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('filename'));
  711. $filepath = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $filename;
  712. if (unlink($filepath)) {
  713. return ['code' => 0, 'msg' => 'success'];
  714. } else {
  715. return ['code' => 1, 'msg' => 'failed'];
  716. }
  717. }
  718. }
  719. /**
  720. * 图片上传(iframe 页面)
  721. */
  722. public function uploadimg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
  723. $original = false, $infoid = 0, $cjid = 0)
  724. {
  725. View::assign([
  726. 'img_id' => $img_id,
  727. 'thumb' => $thumb,
  728. 'width' => $width,
  729. 'height' => $height,
  730. 'original' => $original,
  731. 'infoid' => $infoid,
  732. 'cjid' => $cjid
  733. ]);
  734. return View::fetch();
  735. }
  736. /**
  737. * 本地图片上传
  738. * @file upload_file 上传的文件
  739. * @param string $img_id 图片ipnut text id 默认值 picture
  740. * @param boolean $thumb 是否制作缩略图
  741. * @param int $width 缩略图最大宽
  742. * @param int $height 缩略图最大高
  743. * @param int $id 信息ID
  744. * @param int $cjid 信息临时ID
  745. * @param bool $saveoriginal 生成缩略图后是否保存原图 默认保存 saveoriginal
  746. */
  747. public function uploadLocalImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
  748. $original = false, $id = 0, $cjid = 0)
  749. {
  750. if ($this->request->isPost()) {
  751. $file = $this->request->file('upload_file');
  752. if ($file) {
  753. try {
  754. validate(
  755. [
  756. 'file' => [
  757. // 限制文件大小(单位b),这里限制为4M
  758. 'fileSize' => 4 * 1024 * 1024,
  759. // 限制文件后缀,多个后缀以英文逗号分割
  760. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  761. ]
  762. ],
  763. [
  764. 'file.fileSize' => '文件太大',
  765. 'file.fileExt' => '不支持的文件后缀',
  766. ]
  767. )->check(['file' => $file]);
  768. $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);
  769. return json([
  770. 'code' => 0,
  771. 'img_id' => $img_id,
  772. 'picname' => $result['picname'],
  773. 'thumb' => $result['thumbname']
  774. ]);
  775. } catch (ValidateException $e) {
  776. $this->error($e->getMessage());
  777. } catch (Exception $e) {
  778. $this->error($e->getMessage());
  779. }
  780. } else {
  781. $this->error('图片不能为空');
  782. }
  783. }
  784. }
  785. /**
  786. * 网络图片上传
  787. * @paran string url_file 网络图片地址
  788. * @param string $img_id 图片ipnut text id 默认值 picture
  789. * @param boolean $water 是否添加水印
  790. * @param boolean $thumb 是否制作缩略图
  791. * @param int $width 缩略图最大宽
  792. * @param int $height 缩略图最大高
  793. * @param bool $overwrite 生成缩略图后是否保存原图
  794. */
  795. public function uploadUrlImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
  796. $original = false, $id = 0, $cjid = 0)
  797. {
  798. if ($this->request->isPost()) {
  799. $urlImg = $this->request->param('url_file');
  800. if ($urlImg) {
  801. try {
  802. $file = FileUtils::downloadUrlImg($urlImg);
  803. $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);
  804. // 删除临时文件
  805. @unlink($file->getRealPath());
  806. return json([
  807. 'code' => 0,
  808. 'img_id' => $img_id,
  809. 'picname' => $result['picname'],
  810. 'thumb' => $result['thumbname']
  811. ]);
  812. } catch (\think\exception\ValidateException $e) {
  813. $this->error($e->getMessage());
  814. }
  815. } else {
  816. $this->error('图片地址不能为空');
  817. }
  818. }
  819. }
  820. /**
  821. * 选择服务器图片
  822. * @paran string online_file 服务器图片地址
  823. * @param string $img_id 图片ipnut text id 默认值 picture
  824. */
  825. public function uploadOnlineImg(string $img_id = 'picture')
  826. {
  827. if ($this->request->isPost()) {
  828. $pathImg = $this->request->param('online_file');
  829. if ($pathImg && file_exists($this->app->getRootPath() . "public" . $pathImg)) {
  830. return json([
  831. 'code' => 0,
  832. 'img_id' => $img_id,
  833. 'picname' => $pathImg,
  834. 'thumb' => ''
  835. ]);
  836. } else {
  837. $this->error('图片地址不存在');
  838. }
  839. }
  840. }
  841. /**
  842. * 判断(远程)文件是否为图片
  843. */
  844. // public function isImg()
  845. // {
  846. // if ($this->request->isAjax()) {
  847. // $filename = $this->request->param('filename');
  848. // $res = \mylib\GetImageByurl::isImg($filename);
  849. // if (!$res) {
  850. // $this->error('该图片不合法');
  851. // } else {
  852. // return ['code'=>2,'msg'=>'图片地址可用'];
  853. // }
  854. // }
  855. // }
  856. /**
  857. * ckeditor 富文本编辑器上传图片
  858. */
  859. public function ckeditorUploadImage()
  860. {
  861. if ($this->request->isPost()) {
  862. $file = $this->request->file('upload');
  863. if ($file) {
  864. try {
  865. validate(
  866. [
  867. 'file' => [
  868. // 限制文件大小(单位b),这里限制为4M
  869. 'fileSize' => 4 * 1024 * 1024,
  870. // 限制文件后缀,多个后缀以英文逗号分割
  871. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  872. ]
  873. ],
  874. [
  875. 'file.fileSize' => '文件太大',
  876. 'file.fileExt' => '不支持的文件后缀',
  877. ]
  878. )->check(['file' => $file]);
  879. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  880. $savename = str_replace('\\', '/', $savename);
  881. $infoid = (int) $this->request->param('infoid');
  882. $cjid = (int) $this->request->param('cjid');
  883. $username = $this->getSysUser()->username;
  884. FileManagerModel::saveFileInfo($file, $savename, $file->getOriginalName(), $infoid, $cjid, $username);
  885. return json([
  886. 'uploaded' => 1,
  887. 'fileName' => basename($savename),
  888. 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename
  889. ]);
  890. } catch (\think\exception\ValidateException $e) {
  891. $this->error($e->getMessage());
  892. return json([
  893. 'uploaded' => 1,
  894. 'error' => ['message' => $e->getMessage()]
  895. ]);
  896. }
  897. } else {
  898. return json([
  899. 'uploaded' => 1,
  900. 'error' => ['message' => '图片不能为空']
  901. ]);
  902. }
  903. }
  904. }
  905. /**
  906. * 处理上传的图片
  907. */
  908. protected function dealUploadImg(UploadedFile $file, $thumb, $width, $height, $original, $id, $cjid): array
  909. {
  910. $urlpath = Config::get('filesystem.disks.public.url');
  911. $username = $this->getSysUser()->username;
  912. $savename = str_replace('\\', '/', $file->hashName());
  913. $originalName = $file->getOriginalName();
  914. $thumbname = "";
  915. if ($thumb == true) {
  916. $image = Image::open($file);
  917. $image->thumb($width, $height, 1);
  918. $ext = $file->extension();
  919. if ($original == true) {
  920. \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
  921. $thumbname = str_replace('.' . $ext, '', $savename) . $this->t_suffix . '.' . $ext;
  922. $savepath = str_replace('\\', '/', Config::get('filesystem.disks.public.root') . '/' . $thumbname);
  923. $image->save($savepath);
  924. FileManagerModel::saveFileInfo($savepath, $thumbname, $originalName, $id, $cjid, $username);
  925. $thumbname = $urlpath . '/' . $thumbname;
  926. } else {
  927. $image->save(Config::get('filesystem.disks.public.root') . '/' . $savename);
  928. }
  929. } else {
  930. \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
  931. }
  932. $fileinfo = FileManagerModel::saveFileInfo($file, $savename, $originalName, $id, $cjid, $username);
  933. return [
  934. 'picname' => $fileinfo->filepath,
  935. 'thumbname' => $thumbname,
  936. ];
  937. }
  938. }
  939. >>>>>>> 78b76253c8ce5873016cf837373af5e30ac80c86