| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
-
- /*
- * Handles all uploads
- */
-
- class Upload {
-
- public function __construct() {
-
- }
-
- private static function generateRandomString($length = 5) {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
-
- public static function handleUpload($file = false) {
- $errors = [];
- $filename = $file['name'];
- $size = $file['size'];
- $tempname = $file['tmp_name'];
- $type = $file['type']; // Unused in current state.
- $ext = explode('.', $file['name']); // end() doesnt like us using explode directly.
- $extension = strtolower(end($ext));
- $randomname = Upload::generateRandomString();
-
- $cwd = getcwd();
- $uploadPath = $cwd . Config::$file_path . $randomname . '.' . $extension;
-
- if (!in_array($extension, Config::$file_types)) {
- // Error! This file does not have an acceptable filetype
- $errors[] = "Filtype ikke accepteret.";
- }
-
- if ($size > 5000000) {
- // We accept a max size of 5 megabytes. (not 5 mebibytes)
- $errors[] = "Fil for stor.";
- }
-
- if (empty($errors)) {
- // Do fileupload
-
- $makeUpload = move_uploaded_file($tempname, $uploadPath);
-
- if (!$makeUpload) {
- $errors[] = "Der opstod en fejl under upload af billedet.";
- } else {
- return Config::$sys_url.'img/uploads/'.$randomname.'.'.$extension;
- }
- }
- }
-
- }
|