FileManager.php 7.9 KB

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