From 44a8ac865ae11ca8beb8fd0410ff8c3f34bb783b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=99=E7=A0=81=E7=94=9F=E8=8A=B1?= <18523774412@qq.com> Date: Fri, 15 Mar 2024 12:08:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E7=B1=BB=E5=85=BC=E5=AE=B9=E5=B8=B8=E9=A9=BB=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/library/Upload.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/app/common/library/Upload.php b/app/common/library/Upload.php index c5ff4f72..ed8d8ba5 100644 --- a/app/common/library/Upload.php +++ b/app/common/library/Upload.php @@ -10,6 +10,7 @@ use think\Exception; use think\facade\Config; use think\file\UploadedFile; use app\common\model\Attachment; +use think\exception\FileException; /** * 上传 @@ -258,8 +259,29 @@ class Upload $saveName = '/' . ltrim($saveName, '/'); $uploadDir = substr($saveName, 0, strripos($saveName, '/') + 1); $fileName = substr($saveName, strripos($saveName, '/') + 1); - $destDir = root_path() . 'public' . str_replace('/', DIRECTORY_SEPARATOR, $uploadDir); + $destDir = Filesystem::fsFit(root_path() . 'public' . $uploadDir); - return $this->file->move($destDir, $fileName); + if (request()->isCgi()) { + return $this->file->move($destDir, $fileName); + } + + set_error_handler(function ($type, $msg) use (&$error) { + $error = $msg; + }); + + if (!is_dir($destDir) && !mkdir($destDir, 0777, true)) { + restore_error_handler(); + throw new FileException(sprintf('Unable to create the "%s" directory (%s)', $destDir, strip_tags($error))); + } + + $destination = $destDir . $fileName; + if (!rename($this->file->getPathname(), $destination)) { + restore_error_handler(); + throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->file->getPathname(), $destination, strip_tags($error))); + } + + restore_error_handler(); + @chmod($destination, 0666 & ~umask()); + return $this->file; } } \ No newline at end of file