Explorar o código

优化:文件上传异常处理

Yahoo %!s(int64=5) %!d(string=hai) anos
pai
achega
64376a6e42
Modificáronse 1 ficheiros con 17 adicións e 3 borrados
  1. 17 3
      app/common/library/storage/engine/Basics.php

+ 17 - 3
app/common/library/storage/engine/Basics.php

@@ -66,13 +66,27 @@ abstract class Basics
     public function setUploadFile(string $name)
     {
         // 接收上传的文件
-        $this->file = Request::instance()->file($name);
-        if (empty($this->file)) {
-            throwError('未找到上传文件的信息');
+        try {
+            $this->file = Request::file($name);
+        } catch (\Exception $e) {
+            $this->throwFileError($e);
         }
+        empty($this->file) && throwError('未找到上传文件的信息');
         return $this;
     }
 
+    /**
+     * 文件异常处理
+     * @param \Exception $e
+     * @throws BaseException
+     */
+    private function throwFileError(\Exception $e)
+    {
+        $maxSize = ini_get('upload_max_filesize');
+        $myMsg = $e->getCode() === 1 ? "上传的文件超出了服务器最大限制: {$maxSize}" : false;
+        throwError($myMsg ?: $e->getMessage());
+    }
+
     /**
      * 设置上传的文件信息 (系统内部上传)
      * @param string $filePath 文件路径