소스 검색

Updated uploadclass - Now uploads files correctly.

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

+ 24
- 13
classes/upload.class.php 파일 보기

@@ -6,22 +6,34 @@
6 6
 
7 7
 class Upload {
8 8
 
9
-    // Acceptable filetypes
10
-    private $filetypes = ['jpeg', 'jpg', 'png', 'svg', 'gif'];
11
-
12
-    private function __construct() {
9
+    public function __construct() {
13 10
         
14 11
     }
15 12
 
13
+    private static function generateRandomString($length = 5) {
14
+        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
15
+        $charactersLength = strlen($characters);
16
+        $randomString = '';
17
+        for ($i = 0; $i < $length; $i++) {
18
+            $randomString .= $characters[rand(0, $charactersLength - 1)];
19
+        }
20
+        return $randomString;
21
+    }
22
+
16 23
     public static function handleUpload($file = false) {
17 24
         $errors = [];
18 25
         $filename = $file['name'];
19 26
         $size = $file['size'];
20
-        $tempname = $file['tempname'];
21
-        $type = $file['type'];
22
-        $extension = strtolower(end(explode('.', $filename)));
27
+        $tempname = $file['tmp_name'];
28
+        $type = $file['type']; // Unused in current state.
29
+        $ext = explode('.', $file['name']); // end() doesnt like us using explode directly.
30
+        $extension = strtolower(end($ext));
31
+        $randomname = Upload::generateRandomString();
23 32
 
24
-        if (!in_array($extension, $this->filetypes)) {
33
+        $cwd = getcwd();
34
+        $uploadPath = $cwd . Config::$file_path . $randomname . '.' . $extension;
35
+
36
+        if (!in_array($extension, Config::$file_types)) {
25 37
             // Error! This file does not have an acceptable filetype
26 38
             $errors[] = "Filtype ikke accepteret.";
27 39
         }
@@ -33,15 +45,14 @@ class Upload {
33 45
 
34 46
         if (empty($errors)) {
35 47
             // Do fileupload
36
-            
37
-            $makeUpload = move_uploaded_file($tempname, Config::$file_path);
38
-            
48
+
49
+            $makeUpload = move_uploaded_file($tempname, $uploadPath);
50
+
39 51
             if (!$makeUpload) {
40 52
                 $errors[] = "Der opstod en fejl under upload af billedet.";
41 53
             } else {
42
-                return true;
54
+                return Config::$sys_url.'img/uploads/'.$randomname.'.'.$extension;
43 55
             }
44
-            
45 56
         }
46 57
     }
47 58
 

Loading…
취소
저장