FileManager.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. use app\utils\ReUtils;
  24. class FileManager extends Base
  25. {
  26. protected $modelName = 'FileManager';
  27. public function index()
  28. {
  29. return "error";
  30. }
  31. /**
  32. * ckeditor 富文本编辑器上传图片
  33. */
  34. public function ckeditorUploadImage()
  35. {
  36. if ($this->request->isPost()) {
  37. $file = $this->request->file('upload');
  38. if ($file) {
  39. try {
  40. validate(
  41. [
  42. 'file' => [
  43. // 限制文件大小(单位b),这里限制为4M
  44. 'fileSize' => 4 * 1024 * 1024,
  45. // 限制文件后缀,多个后缀以英文逗号分割
  46. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  47. ]
  48. ],
  49. [
  50. 'file.fileSize' => '文件太大',
  51. 'file.fileExt' => '不支持的文件后缀',
  52. ]
  53. )->check(['file' => $file]);
  54. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  55. return json([
  56. 'uploaded' => 1,
  57. 'fileName' => basename($savename),
  58. 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename
  59. ]);
  60. } catch (\think\exception\ValidateException $e) {
  61. $this->error($e->getMessage());
  62. return json([
  63. 'uploaded' => 0,
  64. 'error' => ['message' => $e->getMessage()]
  65. ]);
  66. }
  67. } else {
  68. return json([
  69. 'uploaded' => 0,
  70. 'error' => ['message' => '图片不能为空']
  71. ]);
  72. }
  73. }
  74. }
  75. /**
  76. * 本地图片上传
  77. * @param file upload_file 上传的文件
  78. */
  79. public function uploadImage()
  80. {
  81. if ($this->request->isPost()) {
  82. $file = $this->request->file('upload_file');
  83. if ($file) {
  84. try {
  85. validate(
  86. [
  87. 'file' => [
  88. // 限制文件大小(单位b),这里限制为4M
  89. 'fileSize' => 4 * 1024 * 1024,
  90. // 限制文件后缀,多个后缀以英文逗号分割
  91. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  92. ]
  93. ],
  94. [
  95. 'file.fileSize' => '文件太大',
  96. 'file.fileExt' => '不支持的文件后缀',
  97. ]
  98. )->check(['file' => $file]);
  99. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  100. $urlpath = Config::get('filesystem.disks.public.url');
  101. $pictureurl = str_replace("\\", "/", $urlpath . '/' . $savename);
  102. return ReUtils::result([
  103. 'filename' => $pictureurl
  104. ]);
  105. } catch (ValidateException $e) {
  106. return ReUtils::error($e->getMessage());
  107. } catch (Exception $e) {
  108. return ReUtils::error($e->getMessage());
  109. }
  110. } else {
  111. return ReUtils::error('图片不能为空');
  112. }
  113. }
  114. }
  115. /**
  116. * 文件上传
  117. */
  118. public function uploadFile()
  119. {
  120. $files = $this->request->file('upload_file');
  121. if ($files) {
  122. try {
  123. validate(
  124. [
  125. 'file' => [
  126. // 限制文件大小(单位b),这里限制为10M
  127. 'fileSize' => 10 * 1024 * 1024,
  128. // 限制文件后缀,多个后缀以英文逗号分割
  129. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif,pdf,doc,docx,xls,xlsx,ppt,pptx,txt'
  130. ]
  131. ],
  132. [
  133. 'file.fileSize' => '文件太大',
  134. 'file.fileExt' => '不支持的文件后缀',
  135. ]
  136. )->check(['file' => $files]);
  137. $savenames = [];
  138. $uploadFiles = [];
  139. if (!is_array($files) && $files instanceof UploadedFile) {
  140. $uploadFiles[] = $files;
  141. } else {
  142. $uploadFiles = $files;
  143. }
  144. $url = Config::get('filesystem.disks.public.url');
  145. foreach ($uploadFiles as $file) {
  146. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  147. $savenames[] = $url . '/' . $savename;
  148. }
  149. return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
  150. } catch (ValidateException $e) {
  151. $this->error($e->getMessage());
  152. }
  153. } else {
  154. $this->error('图片不能为空');
  155. }
  156. }
  157. /**
  158. * 网络图片上传
  159. * @paran string url_file 网络图片地址
  160. * @param string $img_id 图片ipnut text id 默认值 picture
  161. * @param boolean $water 是否添加水印
  162. * @param boolean $thumb 是否制作缩略图
  163. * @param int $width 缩略图最大宽
  164. * @param int $height 缩略图最大高
  165. * @param bool $overwrite 生成缩略图后是否保存原图
  166. */
  167. public function uploadUrlImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
  168. $original = false, $id = 0, $cjid = 0)
  169. {
  170. if ($this->request->isPost()) {
  171. $urlImg = $this->request->param('url_file');
  172. if ($urlImg) {
  173. try {
  174. $file = FileFacade::downloadUrlImg($urlImg);
  175. $urlpath = Config::get('filesystem.disks.public.url');
  176. $username = $this->getSysUser()->username;
  177. $savename = str_replace('\\', '/', $file->hashName());
  178. $originalName = $file->getOriginalName();
  179. $thumbname = "";
  180. \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
  181. $result = [
  182. 'picname' => $savename,
  183. 'thumbname' => $thumbname,
  184. ];
  185. // 删除临时文件
  186. @unlink($file->getRealPath());
  187. return json([
  188. 'code' => 0,
  189. 'img_id' => $img_id,
  190. 'picname' => $result['picname'],
  191. 'thumb' => $result['thumbname']
  192. ]);
  193. } catch (\think\exception\ValidateException $e) {
  194. $this->error($e->getMessage());
  195. }
  196. } else {
  197. $this->error('图片地址不能为空');
  198. }
  199. }
  200. }
  201. }