浏览代码

Update Core\File

You can set the maximum allowed file size for save. Use for avatars.
Visman 4 年之前
父节点
当前提交
7831282422
共有 1 个文件被更改,包括 14 次插入2 次删除
  1. 14 2
      app/Core/File.php

+ 14 - 2
app/Core/File.php

@@ -223,7 +223,7 @@ class File
     /**
      * Сохраняет файл по указанному шаблону пути
      */
-    public function toFile(string $path): bool
+    public function toFile(string $path, int $maxSize = null): bool
     {
         $info = $this->pathinfo($path);
 
@@ -253,10 +253,22 @@ class File
         $path = $info['dirname'] . $info['filename'] . '.' . $info['extension'];
 
         if ($this->fileProc($path)) {
+            $this->size = \filesize($path);
+
+            if (
+                null !== $maxSize
+                && $this->size > $maxSize
+            ) {
+                $this->error = 'File is larger than the allowed size';
+
+                @\unlink($path);
+
+                return false;
+            }
+
             $this->path = $path;
             $this->name = $info['filename'];
             $this->ext  = $info['extension'];
-            $this->size = \filesize($path);
 
             return true;
         } else {