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 queryList() { $param = $this->request->param(); $list = FileManagerModel::queryList($param); return json(['code'=>0, 'list'=>$list]); } /** * 浏览文件 */ 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); // halt($activeurl); if ($this->request->isAjax()) { return json([ 'code' => 0, 'dirs' => $dirs, 'files' => $files, 'counts' => $counts, 'activeurl' => $activeurl, 'activepath'=> $activepath, ]); } else { 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'); 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']; } } } /** * 图片上传(iframe 页面) */ public function uploadimg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300, $original = false, $infoid = 0, $cjid = 0) { View::assign([ 'img_id' => $img_id, 'thumb' => $thumb, 'width' => $width, 'height' => $height, 'original' => $original, 'infoid' => $infoid, 'cjid' => $cjid ]); return View::fetch(); } /** * 本地图片上传 * @file upload_file 上传的文件 * @param string $img_id 图片ipnut text id 默认值 picture * @param boolean $thumb 是否制作缩略图 * @param int $width 缩略图最大宽 * @param int $height 缩略图最大高 * @param int $id 信息ID * @param int $cjid 信息临时ID * @param bool $saveoriginal 生成缩略图后是否保存原图 默认保存 saveoriginal */ public function uploadLocalImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300, $original = false, $id = 0, $cjid = 0) { 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]); $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid); return json([ 'code' => 0, 'img_id' => $img_id, 'picname' => $result['picname'], 'thumb' => $result['thumbname'] ]); } catch (ValidateException $e) { $this->error($e->getMessage()); } catch (Exception $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 uploadUrlImg(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 = FileUtils::downloadUrlImg($urlImg); $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid); // 删除临时文件 @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('图片地址不能为空'); } } } /** * 选择服务器图片 * @paran string online_file 服务器图片地址 * @param string $img_id 图片ipnut text id 默认值 picture */ public function uploadOnlineImg(string $img_id = 'picture') { if ($this->request->isPost()) { $pathImg = $this->request->param('online_file'); if ($pathImg && file_exists($this->app->getRootPath() . "public" . $pathImg)) { return json([ 'code' => 0, 'img_id' => $img_id, 'picname' => $pathImg, 'thumb' => '' ]); } else { $this->error('图片地址不存在'); } } } /** * 判断(远程)文件是否为图片 */ // 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'=>'图片地址可用']; // } // } // } /** * 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); $savename = str_replace('\\', '/', $savename); $infoid = (int) $this->request->param('infoid'); $cjid = (int) $this->request->param('cjid'); $username = $this->getSysUser()->username; FileManagerModel::saveFileInfo($file, $savename, $file->getOriginalName(), $infoid, $cjid, $username); 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' => '图片不能为空'] ]); } } } /** * 处理上传的图片 */ protected function dealUploadImg(UploadedFile $file, $thumb, $width, $height, $original, $id, $cjid): array { $urlpath = Config::get('filesystem.disks.public.url'); $username = $this->getSysUser()->username; $savename = str_replace('\\', '/', $file->hashName()); $originalName = $file->getOriginalName(); $thumbname = ""; if ($thumb == true) { $image = Image::open($file); $image->thumb($width, $height, 1); $ext = $file->extension(); if ($original == true) { \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename); $thumbname = str_replace('.' . $ext, '', $savename) . $this->t_suffix . '.' . $ext; $savepath = str_replace('\\', '/', Config::get('filesystem.disks.public.root') . '/' . $thumbname); $image->save($savepath); FileManagerModel::saveFileInfo($savepath, $thumbname, $originalName, $id, $cjid, $username); $thumbname = $urlpath . '/' . $thumbname; } else { $image->save(Config::get('filesystem.disks.public.root') . '/' . $savename); } } else { \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename); } $fileinfo = FileManagerModel::saveFileInfo($file, $savename, $originalName, $id, $cjid, $username); return [ 'picname' => $fileinfo->filepath, 'thumbname' => $thumbname, ]; } }