FileManager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * upload文件管理
  5. *
  6. * @version 0.0.0
  7. * @author by huwhois
  8. * @time 2017/11/27
  9. */
  10. namespace app\controller\sys;
  11. use Exception;
  12. use UnexpectedValueException;
  13. use DirectoryIterator;
  14. use think\facade\View;
  15. use think\facade\Config;
  16. use think\File;
  17. use think\Image;
  18. use think\exception\ValidateException;
  19. use think\file\UploadedFile;
  20. use app\common\service\FileService;
  21. use app\model\FileManager as FileManagerModel;
  22. use app\facade\FileFacade;
  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. return "error";
  33. }
  34. /**
  35. * ckeditor 富文本编辑器上传图片
  36. */
  37. public function ckeditorUploadImage()
  38. {
  39. if ($this->request->isPost()) {
  40. $file = $this->request->file('upload');
  41. if ($file) {
  42. try {
  43. validate(
  44. [
  45. 'file' => [
  46. // 限制文件大小(单位b),这里限制为4M
  47. 'fileSize' => 4 * 1024 * 1024,
  48. // 限制文件后缀,多个后缀以英文逗号分割
  49. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  50. ]
  51. ],
  52. [
  53. 'file.fileSize' => '文件太大',
  54. 'file.fileExt' => '不支持的文件后缀',
  55. ]
  56. )->check(['file' => $file]);
  57. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  58. $savename = str_replace('\\', '/', $savename);
  59. $infoid = (int) $this->request->param('infoid');
  60. $cjid = (int) $this->request->param('cjid');
  61. $username = $this->getSysUser()->username;
  62. FileManagerModel::saveFileInfo($file, $savename, $file->getOriginalName(), $infoid, $cjid, $username);
  63. return json([
  64. 'uploaded' => 1,
  65. 'fileName' => basename($savename),
  66. 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename
  67. ]);
  68. } catch (\think\exception\ValidateException $e) {
  69. $this->error($e->getMessage());
  70. return json([
  71. 'uploaded' => 1,
  72. 'error' => ['message' => $e->getMessage()]
  73. ]);
  74. }
  75. } else {
  76. return json([
  77. 'uploaded' => 1,
  78. 'error' => ['message' => '图片不能为空']
  79. ]);
  80. }
  81. }
  82. }
  83. /**
  84. * 文件上传
  85. */
  86. public function uploadFile()
  87. {
  88. $files = $this->request->file('upload_file');
  89. if ($files) {
  90. try {
  91. validate(
  92. [
  93. 'file' => [
  94. // 限制文件大小(单位b),这里限制为10M
  95. 'fileSize' => 10 * 1024 * 1024,
  96. // 限制文件后缀,多个后缀以英文逗号分割
  97. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif,pdf,doc,docx,xls,xlsx,ppt,pptx,txt'
  98. ]
  99. ],
  100. [
  101. 'file.fileSize' => '文件太大',
  102. 'file.fileExt' => '不支持的文件后缀',
  103. ]
  104. )->check(['file' => $files]);
  105. $savenames = [];
  106. $uploadFiles = [];
  107. if (!is_array($files) && $files instanceof UploadedFile) {
  108. $uploadFiles[] = $files;
  109. } else {
  110. $uploadFiles = $files;
  111. }
  112. $url = Config::get('filesystem.disks.public.url');
  113. foreach ($uploadFiles as $file) {
  114. $fileinfo = new FileManagerModel();
  115. $fileinfo->title = $file->getOriginalName();
  116. $fileinfo->filesize = $file->getSize();
  117. $fileinfo->filetime = $file->getCTime();
  118. $fileinfo->fileextension = $file->extension();
  119. $fileinfo->username = $this->getSysUser()->username;
  120. $fileinfo->hash_md5 = $file->md5();
  121. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  122. $fileinfo->filepath = $url . '/' . $savename;
  123. $fileinfo->save();
  124. $savenames[] = $fileinfo->filepath;
  125. }
  126. return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
  127. } catch (ValidateException $e) {
  128. $this->error($e->getMessage());
  129. }
  130. } else {
  131. $this->error('图片不能为空');
  132. }
  133. }
  134. /**
  135. * 本地图片上传
  136. * @file upload_file 上传的文件
  137. * @param string $img_id 图片ipnut text id 默认值 picture
  138. * @param boolean $thumb 是否制作缩略图
  139. * @param int $width 缩略图最大宽
  140. * @param int $height 缩略图最大高
  141. * @param int $id 信息ID
  142. * @param int $cjid 信息临时ID
  143. * @param bool $saveoriginal 生成缩略图后是否保存原图 默认保存 saveoriginal
  144. */
  145. public function uploadLocalImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
  146. $original = false, $id = 0, $cjid = 0)
  147. {
  148. if ($this->request->isPost()) {
  149. $file = $this->request->file('upload_file');
  150. if ($file) {
  151. try {
  152. validate(
  153. [
  154. 'file' => [
  155. // 限制文件大小(单位b),这里限制为4M
  156. 'fileSize' => 4 * 1024 * 1024,
  157. // 限制文件后缀,多个后缀以英文逗号分割
  158. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  159. ]
  160. ],
  161. [
  162. 'file.fileSize' => '文件太大',
  163. 'file.fileExt' => '不支持的文件后缀',
  164. ]
  165. )->check(['file' => $file]);
  166. $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);
  167. return json([
  168. 'code' => 0,
  169. 'img_id' => $img_id,
  170. 'picname' => $result['picname'],
  171. 'thumb' => $result['thumbname']
  172. ]);
  173. } catch (ValidateException $e) {
  174. $this->error($e->getMessage());
  175. } catch (Exception $e) {
  176. $this->error($e->getMessage());
  177. }
  178. } else {
  179. $this->error('图片不能为空');
  180. }
  181. }
  182. }
  183. /**
  184. * 网络图片上传
  185. * @paran string url_file 网络图片地址
  186. * @param string $img_id 图片ipnut text id 默认值 picture
  187. * @param boolean $water 是否添加水印
  188. * @param boolean $thumb 是否制作缩略图
  189. * @param int $width 缩略图最大宽
  190. * @param int $height 缩略图最大高
  191. * @param bool $overwrite 生成缩略图后是否保存原图
  192. */
  193. public function uploadUrlImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
  194. $original = false, $id = 0, $cjid = 0)
  195. {
  196. if ($this->request->isPost()) {
  197. $urlImg = $this->request->param('url_file');
  198. if ($urlImg) {
  199. try {
  200. $file = FileFacade::downloadUrlImg($urlImg);
  201. $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);
  202. // 删除临时文件
  203. @unlink($file->getRealPath());
  204. return json([
  205. 'code' => 0,
  206. 'img_id' => $img_id,
  207. 'picname' => $result['picname'],
  208. 'thumb' => $result['thumbname']
  209. ]);
  210. } catch (\think\exception\ValidateException $e) {
  211. $this->error($e->getMessage());
  212. }
  213. } else {
  214. $this->error('图片地址不能为空');
  215. }
  216. }
  217. }
  218. /**
  219. * 处理上传的图片
  220. */
  221. protected function dealUploadImg(UploadedFile $file, $thumb, $width, $height, $original, $id, $cjid): array
  222. {
  223. $urlpath = Config::get('filesystem.disks.public.url');
  224. $username = $this->getSysUser()->username;
  225. $savename = str_replace('\\', '/', $file->hashName());
  226. $originalName = $file->getOriginalName();
  227. $thumbname = "";
  228. if ($thumb == true) {
  229. $image = Image::open($file);
  230. $image->thumb($width, $height, 1);
  231. $ext = $file->extension();
  232. if ($original == true) {
  233. \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
  234. $thumbname = str_replace('.' . $ext, '', $savename) . $this->t_suffix . '.' . $ext;
  235. $savepath = str_replace('\\', '/', Config::get('filesystem.disks.public.root') . '/' . $thumbname);
  236. $image->save($savepath);
  237. FileManagerModel::saveFileInfo($savepath, $thumbname, $originalName, $id, $cjid, $username);
  238. $thumbname = $urlpath . '/' . $thumbname;
  239. } else {
  240. $image->save(Config::get('filesystem.disks.public.root') . '/' . $savename);
  241. }
  242. } else {
  243. \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
  244. }
  245. $fileinfo = FileManagerModel::saveFileInfo($file, $savename, $originalName, $id, $cjid, $username);
  246. return [
  247. 'picname' => $fileinfo->filepath,
  248. 'thumbname' => $thumbname,
  249. ];
  250. }
  251. }