123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- declare(strict_types=1);
- /**
- * upload文件管理
- *
- * @version 0.0.0
- * @author by huwhois
- * @time 2017/11/27
- */
- namespace app\controller\sys;
- use Exception;
- use UnexpectedValueException;
- use DirectoryIterator;
- use think\facade\View;
- use think\facade\Config;
- use think\File;
- use think\Image;
- use think\exception\ValidateException;
- use think\file\UploadedFile;
- use app\common\service\FileService;
- use app\model\FileManager as FileManagerModel;
- use app\facade\FileFacade;
- use app\utils\ReUtils;
- class FileManager extends Base
- {
- protected $modelName = 'FileManager';
-
- public function index()
- {
- return "error";
- }
- /**
- * ckeditor 富文本编辑器上传图片
- */
- public function ckeditorUploadImage()
- {
- if ($this->request->isPost()) {
- $file = $this->request->file('upload');
- if ($file) {
- try {
- validate(
- [
- 'file' => [
- // 限制文件大小(单位b),这里限制为4M
- 'fileSize' => 4 * 1024 * 1024,
- // 限制文件后缀,多个后缀以英文逗号分割
- 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
- ]
- ],
- [
- 'file.fileSize' => '文件太大',
- 'file.fileExt' => '不支持的文件后缀',
- ]
- )->check(['file' => $file]);
- $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
-
- $urlpath = Config::get('filesystem.disks.public.url');
- $pictureurl = str_replace("\\", "/", $urlpath . '/' . $savename);
- return json([
- 'uploaded' => 1,
- 'fileName' => basename($savename),
- 'url' => $pictureurl
- ]);
- } catch (\think\exception\ValidateException $e) {
- $this->error($e->getMessage());
- return json([
- 'uploaded' => 0,
- 'error' => ['message' => $e->getMessage()]
- ]);
- }
- } else {
- return json([
- 'uploaded' => 0,
- 'error' => ['message' => '图片不能为空']
- ]);
- }
- }
- }
- /**
- * 本地图片上传
- * @param file upload_file 上传的文件
- */
- public function uploadImage()
- {
- if ($this->request->isPost()) {
- $file = $this->request->file('upload_file');
- if ($file) {
- try {
- validate(
- [
- 'file' => [
- // 限制文件大小(单位b),这里限制为4M
- 'fileSize' => 4 * 1024 * 1024,
- // 限制文件后缀,多个后缀以英文逗号分割
- 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
- ]
- ],
- [
- 'file.fileSize' => '文件太大',
- 'file.fileExt' => '不支持的文件后缀',
- ]
- )->check(['file' => $file]);
- $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
- $urlpath = Config::get('filesystem.disks.public.url');
- $pictureurl = str_replace("\\", "/", $urlpath . '/' . $savename);
- return ReUtils::result([
- 'filename' => $pictureurl
- ]);
- } catch (ValidateException $e) {
- return ReUtils::error($e->getMessage());
- } catch (Exception $e) {
- return ReUtils::error($e->getMessage());
- }
- } else {
- return ReUtils::error('图片不能为空');
- }
- }
- }
- /**
- * 文件上传
- */
- public function uploadFile()
- {
- $files = $this->request->file('upload_file');
- if ($files) {
- try {
- validate(
- [
- 'file' => [
- // 限制文件大小(单位b),这里限制为10M
- 'fileSize' => 10 * 1024 * 1024,
- // 限制文件后缀,多个后缀以英文逗号分割
- 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif,pdf,doc,docx,xls,xlsx,ppt,pptx,txt'
- ]
- ],
- [
- 'file.fileSize' => '文件太大',
- 'file.fileExt' => '不支持的文件后缀',
- ]
- )->check(['file' => $files]);
- $savenames = [];
- $uploadFiles = [];
-
- if (!is_array($files) && $files instanceof UploadedFile) {
- $uploadFiles[] = $files;
- } else {
- $uploadFiles = $files;
- }
- $url = Config::get('filesystem.disks.public.url');
- foreach ($uploadFiles as $file) {
- $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
- $savenames[] = $url . '/' . $savename;
- }
- return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
- } catch (ValidateException $e) {
- $this->error($e->getMessage());
- }
- } else {
- $this->error('图片不能为空');
- }
- }
- /**
- * 网络图片上传
- * @paran string url_file 网络图片地址
- * @param string $img_id 图片ipnut text id 默认值 picture
- * @param boolean $water 是否添加水印
- * @param boolean $thumb 是否制作缩略图
- * @param int $width 缩略图最大宽
- * @param int $height 缩略图最大高
- * @param bool $overwrite 生成缩略图后是否保存原图
- */
- public function uploadUrlImage(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
- $original = false, $id = 0, $cjid = 0)
- {
- if ($this->request->isPost()) {
- $urlImg = $this->request->param('url_file');
- if ($urlImg) {
- try {
- $file = FileFacade::downloadUrlImg($urlImg);
-
- $urlpath = Config::get('filesystem.disks.public.url');
- $username = $this->getSysUser()->username;
-
- $savename = str_replace('\\', '/', $file->hashName());
-
- $originalName = $file->getOriginalName();
-
- $thumbname = "";
-
- \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
- $result = [
- 'picname' => $savename,
- 'thumbname' => $thumbname,
- ];
- // 删除临时文件
- @unlink($file->getRealPath());
- return json([
- 'code' => 0,
- 'img_id' => $img_id,
- 'picname' => $result['picname'],
- 'thumb' => $result['thumbname']
- ]);
- } catch (\think\exception\ValidateException $e) {
- $this->error($e->getMessage());
- }
- } else {
- $this->error('图片地址不能为空');
- }
- }
- }
- }
|