瀏覽代碼

Update:设置上传文件大小限制

Jinhui Zhu 5 年之前
父節點
當前提交
b70a90833f
共有 3 個文件被更改,包括 36 次插入13 次删除
  1. 1 0
      src/Language/i18n/CN/resource.php
  2. 13 12
      src/Language/i18n/EN/resource.php
  3. 22 1
      src/Library/Upload.php

+ 1 - 0
src/Language/i18n/CN/resource.php

@@ -12,4 +12,5 @@ return array(
     '100008' => '文件上传失败',
     '100009' => '验证码错误',
 	'100010' => '错误的日志类型 %s ',
+    '100011' => '文件尺寸超出大小',
 );

+ 13 - 12
src/Language/i18n/EN/resource.php

@@ -1,15 +1,16 @@
 <?php
 return array(
-    '10001' => '两个值不相同',
-    '100000' => '参数错误',
-    '100001' => '未设置资源文件',
-    '100002' => '未设置标题',
-    '100003' => '未设之资源',
-    '100004' => '参数指定错误',
-    '100005' => '文件移动失败',
-    '100006' => '文件夹不存在',
-    '100007' => '创建文件夹失败',
-    '100008' => '文件上传失败',
-    '100009' => '验证码错误',
-	'100010' => '错误的日志类型 %s ',
+    '10001' => 'The two values are different',
+    '100000' => 'Parameter error',
+    '100001' => 'No resource file set',
+    '100002' => 'No title set',
+    '100003' => 'No resources set up',
+    '100004' => 'Error in parameter assignment',
+    '100005' => 'File movement failed',
+    '100006' => 'Folder does not exist',
+    '100007' => 'Failed to create folder',
+    '100008' => 'File upload faile',
+    '100009' => 'Verification code error',
+    '100010' => 'Log format is error: %s ',
+    '100011' => 'File is too big',
 );

+ 22 - 1
src/Library/Upload.php

@@ -234,7 +234,7 @@ class Upload
         'epub' => 'application/epub+zip',
         'mobi' => 'application/octet-stream',
     );
-    
+    public $size = 0;
     public function __construct()
     {
     
@@ -249,6 +249,10 @@ class Upload
     {
         $this->allowed = $array;
     }
+
+    public function setMaxSize($size = 1024*1024) {
+        $this->size = $size;
+    }
     
     /**
      * setError
@@ -328,6 +332,14 @@ class Upload
             'error' => 0,
             'size' => filesize($tmpFile)
         );
+        if($this->size > 0 && $file['size'] > $this->size) {
+
+            $data['code'] = 1407;
+            $data['src'] = '';
+            $data['uploadMaxSize'] = $this->size;
+            $data['msg'] = 'File is too big';
+            return $data;
+        }
         return $this->deal($file, $configure);
     }
     
@@ -368,6 +380,15 @@ class Upload
             $this->setError($index, 100008);
             return $data;
         }
+        if($this->size > 0 && $files['size'] > $this->size) {
+            $data['code'] = 1;
+            $data['src'] = '';
+            $data['size'] = $files['size'];
+            $data['uploadMaxSize'] = $this->size;
+            $data['msg'] = $this->errorMessage[1];
+            $this->setError($index, 100011);
+            return $data;
+        }
         $extension = pathinfo($files['name'], PATHINFO_EXTENSION);
         $extension = strtolower($extension);
         if (!in_array($files['type'], $this->mine) || !isset($this->mine[$extension])) {