123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- <?php
- declare(strict_types=1);
- /**
- * upload文件管理
- *
- * @version 0.0.0
- * @author by huwhois
- * @time 2017/11/27
- */
- namespace app\sys\controller;
- use Exception;
- use think\facade\View;
- use think\File;
- use think\Image;
- use think\facade\Config;
- use app\common\service\FileService;
- use app\common\model\FileManager as FileManagerModel;
- class FileManager extends Base
- {
- protected $t_suffix = '_thumb';
- protected $width = 400; // 缩略图高度
- protected $height = 300;
- /**
- * 图片列表
- */
- // public function index($mod=0)
- // {
- // }
- public function index()
- {
- // $data = $this->explorer();
- // View::assign('dirs', $data['dirs']);
- // View::assign('files', $data['files']);
- // View::assign('counts', $data['counts']);
- // View::assign('activeurl', $data['activeurl']);
- // View::assign('activepath', $data['activepath']);
- $param = $this->request->param();
- $param['limit'] = isset($param['limit']) ? (int) $param['limit'] : Config::get('app.page_size');
- $list = FileManagerModel::queryPage($param);
- View::assign('list', $list);
- return View::fetch();
- }
- public function uploadFile()
- {
- $files = $this->request->file('upload_file');
- $savename = [];
-
- foreach($files as $file){
- $savename[] = \think\facade\Filesystem::disk('public')->putFile('/', $file);
- }
- }
- /**
- * 图片上传
- * @file upload_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 uploadimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
- {
- 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]);
- $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);
- return array_merge([
- 'code' => 0,
- 'img_id' => $img_id,
- 'picture_url' => Config::get('filesystem.disks.public.url') . '/']
- , $arr);
- } catch (\think\exception\ValidateException $e) {
- $this->error($e->getMessage());
- }
- } else {
- $this->error('图片不能为空');
- }
- } else {
- View::assign('img_id', $img_id);
- View::assign('water', $water);
- View::assign('thumb', $thumb);
- View::assign('width', $width);
- View::assign('height', $height);
- View::assign('overwrite', $overwrite);
- return View::fetch();
- }
- }
- /**
- * 图片上传
- * @file upload_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 uploadurlimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
- {
- if ($this->request->isPost()) {
- $urlImg = $this->request->param('url_file');
- if ($urlImg) {
- try {
- $fileService = new FileService();
- $file = $fileService->urlImg($urlImg);
- $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);
- @unlink($file->realPath);
- return array_merge([
- 'code' => 0,
- 'img_id' => $img_id,
- 'picture_url' => Config::get('filesystem.disks.public.url') . '/']
- , $arr);
- } catch (\think\exception\ValidateException $e) {
- $this->error($e->getMessage());
- }
- } else {
- $this->error('图片地址不能为空');
- }
- }
- }
- public function uploadonlineimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
- {
- if ($this->request->isPost()) {
- $pathImg = $this->request->param('online_file');
- if ($pathImg && file_exists($this->app->getRootPath() . "public" . $pathImg)) {
- $picname = $pathImg;
- $thumbname = "";
-
- if ($thumb) {
- if (stripos($picname, $this->t_suffix)) {
- $thumbname = $pathImg;
- } else {
- try {
- $file = new File($this->app->getRootPath() . "public" . $pathImg);
-
- $ext = $file->getExtension();
-
- $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $picname)) . $this->t_suffix . '.' . $ext;
- if (!file_exists($thumbname)) {
- $image = Image::open($file);
-
- $image->thumb($width, $height, 1);
-
- $image->save($this->app->getRootPath() . "public" . $thumbname);
- unset($image);
- }
- if ($overwrite) {
- $picname = $thumbname;
- }
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- }
- return [
- 'code' => 0,
- 'img_id' => $img_id,
- 'picname' => $picname,
- 'thumbname' => $thumbname,
- ];
- } else {
- $this->error('图片地址不存在');
- }
- }
- }
- public function onlineimg($page)
- {
- $files = FileService::getFiles(Config::get('filesystem.disks.public.root'));
- if (!count($files)) {
- return json_encode(array(
- "state" => "no match file",
- "list" => array(),
- "start" => $page,
- "total" => count($files)
- ));
- }
- /* 获取指定范围的列表 */
- $page = $page-1 < 0 ? 0 : (int)$page - 1;
- $size = 20;
- $start = $page * $size;
- $end = $start + $size;
- $len = count($files);
- for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
- $list[] = $files[$i];
- }
- return json([ "code" => 0,
- "list" => $list,
- "page" => $page+1,
- "total" => count($files)
- ]);
- }
- /**
- * 获取当前文件相关信息
- * @return array $data 文件信息数据组
- */
- protected function explorer()
- {
- $param = $this->request->param();
- $activepath = isset($param['activepath']) ? $param['activepath'] : '';
- $inpath = "";
- $inpath = $this->img_path . $activepath;
- $dirhandle = scandir($inpath);
- $dirs = $files = [];
- define('BKM', 1024);
- foreach ($dirhandle as $val) {
- if ($val == "." || $val == "..") {
- continue;
- } elseif (is_dir($inpath . DIRECTORY_SEPARATOR . $val)) {
- $dirs[] = $val;
- } else {
- $arr = [];
- $file = '';
- $file = $inpath . DIRECTORY_SEPARATOR . $val;
- $arr['name'] = $val;
- $arr['extension'] = pathinfo($file, PATHINFO_EXTENSION);
- $filesize = floatval(filesize($file));
- if ($filesize > BKM) {
- $filesize = round($filesize / BKM, 2);
- if ($filesize > BKM) {
- $filesize = round($filesize / BKM, 2);
- $filesize .= "M";
- } else {
- $filesize .= "K";
- }
- } else {
- $filesize .= "B";
- }
- $arr['size'] = $filesize;
- $filetime = filemtime("$file");
- $arr['time'] = date("Y-m-d H:i:s", $filetime);
- $files[] = $arr;
- }
- }
- $counts = count($dirs) + count($files);
- $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);
- $data = [
- 'dirs' => $dirs,
- 'files' => $files,
- 'counts' => $counts,
- 'activeurl' => $activeurl,
- 'activepath' => $activepath,
- ];
- return $data;
- }
- public function delDir()
- {
- if ($this->request->isAjax()) {
- $activepath = $this->request->param('activepath');
- $activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);
- $dir = $this->request->param('dir');
- $dir_name = app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . $activepath . DIRECTORY_SEPARATOR . $dir;
- if (count(scandir($dir_name)) > 2) {
- return ['status' => 1, 'msg' => '不可删除非空目录'];
- }
- if (rmdir($dir_name)) {
- return ['status' => 2, 'msg' => 'success'];
- } else {
- return ['status' => 0, 'msg' => 'failed'];
- }
- }
- }
- public function del()
- {
- if ($this->request->isAjax()) {
- $activepath = $this->request->param('activepath');
- $activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);
- $filename = $this->request->param('filename');
- if (unlink(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . $activepath . DIRECTORY_SEPARATOR . $filename)) {
- return ['status' => 1, 'msg' => 'success'];
- } else {
- return ['status' => 0, 'msg' => 'failed'];
- }
- }
- }
- // public function upload()
- // {
- // if ($this->request->isGet()) {
- // return View::fetch()();
- // } else {
- // $isthumb = $this->request->has('isthumb', 'post') ? $this->request->post('isthumb') : 0;
- // $mode = $this->request->has('formername', 'post') ? $this->request->post('formername') : 0;
- // $file = request()->file('image');
- // $info = $this->saveUpload($file, $mode);
- // if (!$info) {
- // $this->error($info->getError());
- // }
- // $filename = $info->getSaveName();
- // unset($info);
- // if ($isthumb == 1) {
- // $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;
- // $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;
- // $thumbname = $this->makeThumb($filename, $width, $height);
- // if (!$thumbname) {
- // $this->error('缩略图生成失败');
- // }
- // } else {
- // $thumbname = '';
- // }
- // if ($this->request->isAjax()) {
- // return ['code'=>2, 'picname' => $filename, 'thumbname'=>$thumbname];
- // } else {
- // $this->success('上传成功', '/file_manager/index');
- // }
- // }
- // }
- /**
- * 保存上传的图片
- * @param object $file 获取的上传对象
- * @param boolean $mode 是否保存原文件名 0(false), 生成新名称; 1(true), 保留原名
- * @return object 返回文件保存对象
- */
- protected function saveUpload($file, $mode)
- {
- $validate = ['size' => 2097152, 'ext' => 'jpg,png,gif,jpeg'];
- if ($mode) {
- $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads', '');
- } else {
- $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads');
- }
- return $upload;
- }
- /**
- * 站内选择
- * @return void
- */
- public function selectPicture()
- {
- $data = $this->explorer();
- View::assign('data', $data);
- return View::fetch()();
- }
- public function uploadPicture()
- {
- return View::fetch()();
- }
- // public function onlinePicture()
- // {
- // if ($this->request->isAjax()) {
- // $param = $this->request->param();
- // $urlimg = $param['filename'];
- // $download = $param['download'];
- // $isthumb = $param['isthumb'];
- // $formername = $param['formername'];
- // $res = \mylib\GetImageByurl::isImg($urlimg);
- // if (!$res) {
- // $this->error('该图片不合法');
- // } else {
- // if (!$download) {
- // return ['code'=>2,'filename'=>$urlimg];
- // } else {
- // $basepath = \config('url_domain_root') . '/uploads';
- // // var_dump($param);
- // if ($isthumb!=1 && preg_match("#" . $basepath . "#i", $urlimg)) {
- // $this->error('图片已在服务其中, 可直接选用');
- // }
- // // 按文件夹日期夹存放图片
- // $today = date('Ymd', time());
- // $savePath = $this->img_path . $today;
- // if (!is_dir($savePath)) {
- // if (mkdir($savePath) == false) {
- // $this->error('下载失败, 请稍后重试');
- // }
- // }
- // if ($formername==1) {
- // // 获取原文件名
- // $fileinfo = pathinfo($urlimg);
- // if (!isset($fileinfo['extension']) || !in_array($fileinfo['extension'], ['jpg', 'jpeg', 'png', 'gif'])) {
- // $ext = \mylib\GetImageByurl::getEXTENSION($urlimg);
- // }
- // $filename = $fileinfo['basename'] . '.' . $ext;
- // } else {
- // $filename = '';
- // }
- // $filename = \mylib\GetImageByurl::getImageByurl($urlimg, $filename, $savePath);
- // if ($filename) {
- // // 生成缩略图
- // if ($isthumb==1) {
- // $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;
- // $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;
- // $thumbname = $this->makeThumb($today . DIRECTORY_SEPARATOR . $filename, $width, $height);
- // if (!$thumbname) {
- // $this->error('缩略图生成失败');
- // }
- // } else {
- // $thumbname = '';
- // }
- // return ['code'=>2, 'filename' => $today . DIRECTORY_SEPARATOR . $filename, 'thumbname'=>$thumbname];
- // } else {
- // $this->error('图片下载失败');
- // }
- // }
- // }
- // } else {
- // return View::fetch()();
- // }
- // }
- /**
- * 判断(远程)文件是否为图片
- */
- // public function isImg()
- // {
- // if ($this->request->isAjax()) {
- // $filename = $this->request->param('filename');
- // $res = \mylib\GetImageByurl::isImg($filename);
- // if (!$res) {
- // $this->error('该图片不合法');
- // } else {
- // return ['code'=>2,'msg'=>'图片地址可用'];
- // }
- // }
- // }
- /**
- * 处理上传的图片
- */
- protected function dealUploadImg(File $file, $water, $thumb, $width, $height, $overwrite)
- {
- $savename = "";
- $thumbname = "";
- if ($water == true || $thumb == true) {
- $image = Image::open($file);
- if ($water) {
- $type = $this->system->water_type ?: Config::get('filesystem.water.type');
- if ($type == 'water') {
- $watemark = $this->system->watermark ?: Config::get('filesystem.water.watermark');
- $image->water($watemark);
- } else {
- $watetext = $this->system->watertext ?: Config::get('filesystem.water.watertext');
- $image->text($watetext, Config::get('filesystem.water.waterfont'), 30, '#ffffff30');
- }
- }
- $savename = $file->hashName();
- $realPath = Config::get('filesystem.disks.public.root') . '/' . $savename;
- if ($thumb == true) {
- if ($overwrite == true) {
- $image->thumb($width, $height, 1);
- $image->save($realPath);
- } else {
- $image->save($realPath);
- $image->thumb($width, $height, 1);
- $ext = $file->extension();
- $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $savename)) . $this->t_suffix . '.' . $ext;
- // halt(Config::get('filesystem.disks.public.root') .'/' . $thumbname);
- $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);
- }
- } else {
- $image->save($realPath);
- }
- unset($image);
- } else {
- $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
- }
- return [
- 'picname' => str_replace('\\', '/', $savename),
- 'thumbname' => $thumbname
- ];
- }
- /**
- * 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);
- return json([
- 'uploaded' => 1,
- 'fileName' => basename($savename),
- 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename
- ]);
- } catch (\think\exception\ValidateException $e) {
- $this->error($e->getMessage());
- return json([
- 'uploaded' => 1,
- 'error' => ['message'=>$e->getMessage()]
- ]);
- }
- } else {
- return json([
- 'uploaded' => 1,
- 'error' => ['message'=>'图片不能为空']
- ]);
- }
- }
- }
- }
|