소스 검색

Updated uploadclass - Comments

tags/rls1
kenn408k 6 년 전
부모
커밋
1402fe464c
1개의 변경된 파일19개의 추가작업 그리고 17개의 파일을 삭제
  1. 19
    17
      classes/upload.class.php

+ 19
- 17
classes/upload.class.php 파일 보기

@@ -11,46 +11,48 @@ class Upload {
11 11
     }
12 12
 
13 13
     private static function generateRandomString($length = 5) {
14
-        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
15
-        $charactersLength = strlen($characters);
16
-        $randomString = '';
14
+        // Function to generate a random string of 5 chars.
15
+        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // Chars to use
16
+        $charactersLength = strlen($characters); // Check string length
17
+        $randomString = ''; // An empty var to store our randomstring
17 18
         for ($i = 0; $i < $length; $i++) {
19
+            // Here we loop until we filled our string with the wanted length.
18 20
             $randomString .= $characters[rand(0, $charactersLength - 1)];
19 21
         }
20
-        return $randomString;
22
+        return $randomString; // Returns our randomstring.
21 23
     }
22 24
 
23 25
     public static function handleUpload($file = false) {
24
-        $errors = [];
25
-        $filename = $file['name'];
26
-        $size = $file['size'];
27
-        $tempname = $file['tmp_name'];
26
+        $filename = $file['name']; // Get filename
27
+        $size = $file['size']; // Gets filesize
28
+        $tempname = $file['tmp_name']; // Where did php upload our data to?
28 29
         $type = $file['type']; // Unused in current state.
29 30
         $ext = explode('.', $file['name']); // end() doesnt like us using explode directly.
30
-        $extension = strtolower(end($ext));
31
-        $randomname = Upload::generateRandomString();
31
+        $extension = strtolower(end($ext)); // Get the extension of the file.
32
+        $randomname = Upload::generateRandomString(); // We give our file a random name - It will be this.
32 33
 
33
-        $cwd = getcwd();
34
+        $cwd = getcwd(); // Our websites CWD (Current working dir)
34 35
         $uploadPath = $cwd . Config::$file_path . $randomname . '.' . $extension;
35 36
 
36 37
         if (!in_array($extension, Config::$file_types)) {
37 38
             // Error! This file does not have an acceptable filetype
38
-            $errors[] = "Filtype ikke accepteret.";
39
+            return false; // Return false when upload fails.
39 40
         }
40 41
 
41
-        if ($size > 5000000) {
42
-            // We accept a max size of 5 megabytes. (not 5 mebibytes)
43
-            $errors[] = "Fil for stor.";
42
+        if ($size > Config::$file_size) {
43
+            // Filesize is configured in config, defined in bytes.
44
+            return false; // Return false when upload fails.
44 45
         }
45 46
 
46 47
         if (empty($errors)) {
47 48
             // Do fileupload
48
-
49
+            // move_uploaded_file is self-explanatory. We move the file into the correct path.
49 50
             $makeUpload = move_uploaded_file($tempname, $uploadPath);
50 51
 
51 52
             if (!$makeUpload) {
52
-                $errors[] = "Der opstod en fejl under upload af billedet.";
53
+                return false; // Return false when upload fails.
53 54
             } else {
55
+                // Return where the image was uploaded to.
54 56
                 return Config::$sys_url.'img/uploads/'.$randomname.'.'.$extension;
55 57
             }
56 58
         }

Loading…
취소
저장