|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
/**
|
|
@@ -12,38 +13,28 @@ declare(strict_types=1);
|
|
|
namespace app\sys\controller;
|
|
|
|
|
|
use Exception;
|
|
|
+use UnexpectedValueException;
|
|
|
+use DirectoryIterator;
|
|
|
use think\facade\View;
|
|
|
use think\File;
|
|
|
use think\Image;
|
|
|
use think\facade\Config;
|
|
|
+use think\exception\ValidateException;
|
|
|
|
|
|
use app\common\service\FileService;
|
|
|
use app\common\model\FileManager as FileManagerModel;
|
|
|
+use think\file\UploadedFile;
|
|
|
|
|
|
class FileManager extends Base
|
|
|
{
|
|
|
+ protected $modelName = 'FileManager';
|
|
|
protected $t_suffix = '_thumb';
|
|
|
protected $width = 400; // 缩略图高度
|
|
|
protected $height = 300;
|
|
|
-
|
|
|
- /**
|
|
|
- * 图片列表
|
|
|
- */
|
|
|
- // public function index($mod=0)
|
|
|
- // {
|
|
|
-
|
|
|
- // }
|
|
|
+ protected $storage_path = 'public/storage';
|
|
|
|
|
|
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');
|
|
@@ -55,19 +46,294 @@ class FileManager extends Base
|
|
|
return View::fetch();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 浏览文件
|
|
|
+ */
|
|
|
+ public function explorer()
|
|
|
+ {
|
|
|
+ $param = $this->request->param();
|
|
|
+
|
|
|
+ $activepath = isset($param['activepath']) ? $param['activepath'] : '';
|
|
|
+
|
|
|
+ $realpath = $this->app->getRootPath() . $this->storage_path . $activepath;
|
|
|
+
|
|
|
+ try {
|
|
|
+ $dirhandle = new DirectoryIterator($realpath);
|
|
|
+
|
|
|
+ $dirs = $files = [];
|
|
|
+
|
|
|
+ foreach ($dirhandle as $fileInfo) {
|
|
|
+ if($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
|
|
|
+ continue;
|
|
|
+ } elseif ($fileInfo->isDir()) {
|
|
|
+ $dirs[] = $fileInfo->getFilename();
|
|
|
+ } elseif ($fileInfo->isFile()) {
|
|
|
+ $files[] = [
|
|
|
+ 'filename' => $fileInfo->getFilename(),
|
|
|
+ 'extension' => strtolower($fileInfo->getExtension()),
|
|
|
+ 'size' => format_bytes($fileInfo->getSize()),
|
|
|
+ 'time' => $fileInfo->getCTime(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $counts = count($dirs) + count($files);
|
|
|
+
|
|
|
+ $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);
|
|
|
+
|
|
|
+ View::assign([
|
|
|
+ 'dirs' => $dirs,
|
|
|
+ 'files' => $files,
|
|
|
+ 'counts' => $counts,
|
|
|
+ 'activeurl' => $activeurl,
|
|
|
+ 'activepath' => $activepath,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return View::fetch();
|
|
|
+ } catch(UnexpectedValueException $uve) {
|
|
|
+ $this->error($uve->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 附件上传
|
|
|
+ */
|
|
|
public function uploadFile()
|
|
|
{
|
|
|
+ $activepath = $this->request->has('activepath') ? $this->request->param('activepath') : '';
|
|
|
+
|
|
|
$files = $this->request->file('upload_file');
|
|
|
|
|
|
- $savename = [];
|
|
|
-
|
|
|
- foreach($files as $file){
|
|
|
- $savename[] = \think\facade\Filesystem::disk('public')->putFile('/', $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) {
|
|
|
+ $fileinfo = new FileManagerModel();
|
|
|
+
|
|
|
+ $fileinfo->title = $file->getOriginalName();
|
|
|
+ $fileinfo->filesize = $file->getSize();
|
|
|
+ $fileinfo->filetime = $file->getCTime();
|
|
|
+ $fileinfo->fileextension = $file->extension();
|
|
|
+ $fileinfo->username = $this->getSysUser()->username;
|
|
|
+ $fileinfo->hash_md5 = $file->md5();
|
|
|
+
|
|
|
+ if (empty($activepath)) {
|
|
|
+ $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
|
|
|
+
|
|
|
+ $fileinfo->filepath = $url . '/' . $savename;
|
|
|
+ $fileinfo->filename = basename($savename);
|
|
|
+ } else {
|
|
|
+ $savename = $activepath . '/' . $file->hashName();
|
|
|
+ $savepath = $this->app->getRootPath() . $this->storage_path . $savename;
|
|
|
+
|
|
|
+ $file = $file->move($this->app->getRootPath() . $this->storage_path . $activepath, $savepath);
|
|
|
+
|
|
|
+ $fileinfo->filepath = $url . $savename;
|
|
|
+ $fileinfo->filename = $file->hashName();
|
|
|
+ }
|
|
|
+
|
|
|
+ $fileinfo->save();
|
|
|
+
|
|
|
+ $savenames[] = $fileinfo->filepath;
|
|
|
+ }
|
|
|
+
|
|
|
+ return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
|
|
|
+ } catch (ValidateException $e) {
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $this->error('图片不能为空');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改文件title
|
|
|
+ */
|
|
|
+ public function updateFileTitle($id = 0, $filetitle = '')
|
|
|
+ {
|
|
|
+ $fileInfo = FileManagerModel::find($id);
|
|
|
+ if ($fileInfo == null) {
|
|
|
+ return json(['code'=>1, 'fileid='.$id.'不存在']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $fileInfo->title = $filetitle;
|
|
|
+
|
|
|
+ if ($fileInfo->save()) {
|
|
|
+ return json(['code'=>0, 'msg'=>'success']);
|
|
|
+ } else {
|
|
|
+ return json(['code'=>1, 'msg'=>'failed']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除
|
|
|
+ * @param int|array $id info id
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function delete($id)
|
|
|
+ {
|
|
|
+ if ($this->request->isPost()) {
|
|
|
+ $whereOp = '=';
|
|
|
+
|
|
|
+ if (is_array($id)) {
|
|
|
+ $whereOp = 'IN';
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = FileManagerModel::where('fileid', $whereOp, $id)->select();
|
|
|
+
|
|
|
+ foreach ($list as $value) {
|
|
|
+ $file = $this->app->getRootPath() . 'public' . $value->filepath;
|
|
|
+ if (file_exists($file)) {
|
|
|
+ unlink($file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (FileManagerModel::destroy($id)) {
|
|
|
+ return ['code' => 0,'msg'=>'删除成功'];
|
|
|
+ } else {
|
|
|
+ return ['code' => 1,'msg'=>'删除失败'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除空目录
|
|
|
+ */
|
|
|
+ public function deldir()
|
|
|
+ {
|
|
|
+ if ($this->request->isPost()) {
|
|
|
+ $dir = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('dir'));
|
|
|
+
|
|
|
+ $dir_name = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $dir;
|
|
|
+
|
|
|
+ try {
|
|
|
+ rmdir($dir_name);
|
|
|
+ return ['code' => 0, 'msg' => 'success'];
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return ['code' => 1, 'msg' => 'failed, 目录不为空'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件
|
|
|
+ */
|
|
|
+ public function delfile()
|
|
|
+ {
|
|
|
+ if ($this->request->isPost()) {
|
|
|
+ $filename = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('filename'));
|
|
|
+
|
|
|
+ $filepath = $this->app->getRootPath() . $this->storage_path . DIRECTORY_SEPARATOR . $filename;
|
|
|
+
|
|
|
+ if (unlink($filepath)) {
|
|
|
+ return ['code' => 0, 'msg' => 'success'];
|
|
|
+ } else {
|
|
|
+ return ['code' => 1, 'msg' => 'failed'];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 图片上传
|
|
|
+ * 处理上传的图片
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+
|
|
|
+ $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
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 图片上传(iframe 页面)
|
|
|
+ */
|
|
|
+ public function uploadimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
+ {
|
|
|
+ View::assign([
|
|
|
+ 'img_id' => $img_id,
|
|
|
+ 'water' => $water,
|
|
|
+ 'thumb' => $thumb,
|
|
|
+ 'width' => $width,
|
|
|
+ 'height' => $height,
|
|
|
+ 'overwrite' => $overwrite
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return View::fetch();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本地图片上传
|
|
|
* @file upload_file 上传的文件
|
|
|
* @param string $img_id 图片ipnut text id 默认值 picture
|
|
|
* @param boolean $water 是否添加水印
|
|
@@ -76,7 +342,7 @@ class FileManager extends Base
|
|
|
* @param int $height 缩略图最大高
|
|
|
* @param bool $overwrite 生成缩略图后是否保存原图
|
|
|
*/
|
|
|
- public function uploadimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
+ public function uploadLocalImg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
{
|
|
|
if ($this->request->isPost()) {
|
|
|
$file = $this->request->file('upload_file');
|
|
@@ -96,35 +362,30 @@ class FileManager extends Base
|
|
|
'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) {
|
|
|
+ return array_merge(
|
|
|
+ [
|
|
|
+ 'code' => 0,
|
|
|
+ 'img_id' => $img_id,
|
|
|
+ 'picture_url' => Config::get('filesystem.disks.public.url') . '/'
|
|
|
+ ],
|
|
|
+ $arr
|
|
|
+ );
|
|
|
+
|
|
|
+ } catch (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 上传的文件
|
|
|
+ * 网络图片上传
|
|
|
+ * @paran string url_file 网络图片地址
|
|
|
* @param string $img_id 图片ipnut text id 默认值 picture
|
|
|
* @param boolean $water 是否添加水印
|
|
|
* @param boolean $thumb 是否制作缩略图
|
|
@@ -132,7 +393,7 @@ class FileManager extends Base
|
|
|
* @param int $height 缩略图最大高
|
|
|
* @param bool $overwrite 生成缩略图后是否保存原图
|
|
|
*/
|
|
|
- public function uploadurlimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
+ 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');
|
|
@@ -141,17 +402,20 @@ class FileManager extends Base
|
|
|
try {
|
|
|
$fileService = new FileService();
|
|
|
|
|
|
- $file = $fileService->urlImg($urlImg);
|
|
|
+ $file = $fileService->downloadUrlImg($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);
|
|
|
+ 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());
|
|
|
}
|
|
@@ -161,6 +425,16 @@ class FileManager extends Base
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 选择服务器图片
|
|
|
+ * @paran string online_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 uploadonlineimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
{
|
|
|
if ($this->request->isPost()) {
|
|
@@ -170,23 +444,23 @@ class FileManager extends Base
|
|
|
|
|
|
$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);
|
|
|
}
|
|
@@ -225,278 +499,25 @@ class FileManager extends Base
|
|
|
));
|
|
|
}
|
|
|
/* 获取指定范围的列表 */
|
|
|
- $page = $page-1 < 0 ? 0 : (int)$page - 1;
|
|
|
+ $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--){
|
|
|
+ for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
|
|
|
$list[] = $files[$i];
|
|
|
}
|
|
|
|
|
|
- return json([ "code" => 0,
|
|
|
+ return json([
|
|
|
+ "code" => 0,
|
|
|
"list" => $list,
|
|
|
- "page" => $page+1,
|
|
|
+ "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()();
|
|
|
- // }
|
|
|
- // }
|
|
|
-
|
|
|
/**
|
|
|
* 判断(远程)文件是否为图片
|
|
|
*/
|
|
@@ -514,62 +535,6 @@ class FileManager extends Base
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
- /**
|
|
|
- * 处理上传的图片
|
|
|
- */
|
|
|
- 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 富文本编辑器上传图片
|
|
|
*/
|
|
@@ -605,13 +570,13 @@ class FileManager extends Base
|
|
|
$this->error($e->getMessage());
|
|
|
return json([
|
|
|
'uploaded' => 1,
|
|
|
- 'error' => ['message'=>$e->getMessage()]
|
|
|
+ 'error' => ['message' => $e->getMessage()]
|
|
|
]);
|
|
|
}
|
|
|
} else {
|
|
|
return json([
|
|
|
'uploaded' => 1,
|
|
|
- 'error' => ['message'=>'图片不能为空']
|
|
|
+ 'error' => ['message' => '图片不能为空']
|
|
|
]);
|
|
|
}
|
|
|
}
|