"OdenseTrack" is a school assignment/project from AspIT https://aspit.dfine.net/odensetrack
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

upload.class.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * Handles all uploads
  4. */
  5. class Upload {
  6. public function __construct() {
  7. }
  8. private static function generateRandomString($length = 5) {
  9. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  10. $charactersLength = strlen($characters);
  11. $randomString = '';
  12. for ($i = 0; $i < $length; $i++) {
  13. $randomString .= $characters[rand(0, $charactersLength - 1)];
  14. }
  15. return $randomString;
  16. }
  17. public static function handleUpload($file = false) {
  18. $errors = [];
  19. $filename = $file['name'];
  20. $size = $file['size'];
  21. $tempname = $file['tmp_name'];
  22. $type = $file['type']; // Unused in current state.
  23. $ext = explode('.', $file['name']); // end() doesnt like us using explode directly.
  24. $extension = strtolower(end($ext));
  25. $randomname = Upload::generateRandomString();
  26. $cwd = getcwd();
  27. $uploadPath = $cwd . Config::$file_path . $randomname . '.' . $extension;
  28. if (!in_array($extension, Config::$file_types)) {
  29. // Error! This file does not have an acceptable filetype
  30. $errors[] = "Filtype ikke accepteret.";
  31. }
  32. if ($size > 5000000) {
  33. // We accept a max size of 5 megabytes. (not 5 mebibytes)
  34. $errors[] = "Fil for stor.";
  35. }
  36. if (empty($errors)) {
  37. // Do fileupload
  38. $makeUpload = move_uploaded_file($tempname, $uploadPath);
  39. if (!$makeUpload) {
  40. $errors[] = "Der opstod en fejl under upload af billedet.";
  41. } else {
  42. return Config::$sys_url.'img/uploads/'.$randomname.'.'.$extension;
  43. }
  44. }
  45. }
  46. }