"OdenseTrack" is a school assignment/project from AspIT https://aspit.dfine.net/odensetrack
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

handleUpload.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * TinyMCE Image uploader!
  4. */
  5. mb_internal_encoding("utf-8"); // Internal encoding set to UTF-8, should fix some charset issues.
  6. session_start(); // Start PHP session, so we can handle session-data on all pages.
  7. require_once("include.php");
  8. // Get working dir, and file path
  9. $cwd = getcwd();
  10. $imageFolder = $cwd . '/' . Config::$file_path;
  11. reset($_FILES);
  12. $temp = current($_FILES);
  13. if (is_uploaded_file($temp['tmp_name'])) {
  14. if (!User::checkLevel("75")) {
  15. header("HTTP/1.1 403 Permission Denied");
  16. return;
  17. }
  18. // Set required variables.
  19. $ext = explode('.', $temp['name']); // end() doesnt like us using explode directly.
  20. $extension = strtolower(end($ext)); // Get the extension of the file.
  21. $randomname = Upload::generateRandomString(); // We give our file a random name - It will be this.
  22. // Check if filename exists, and regenrate until it doesnt.
  23. while (IS_FILE($cwd . '/' . Config::$file_path . $randomname . '.' . $extension)) {
  24. $randomname = Upload::generateRandomString();
  25. }
  26. // Sanitize input
  27. if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
  28. header("HTTP/1.1 400 Invalid file name.");
  29. return;
  30. }
  31. // Verify extension
  32. if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), Config::$file_types)) {
  33. header("HTTP/1.1 400 Invalid extension.");
  34. return;
  35. }
  36. // Accept upload
  37. $filetowrite = $imageFolder . $randomname .'.'. $extension;
  38. move_uploaded_file($temp['tmp_name'], $filetowrite);
  39. // Respond to the successful upload with JSON. - And add image to database.
  40. Alter::insertImage($randomname . '.' . $extension, $temp['type']);
  41. echo json_encode(array('location' => Config::$sys_url . Config::$file_path . $randomname .'.'. $extension));
  42. } else {
  43. // Notify editor that the upload failed
  44. header("HTTP/1.1 500 Server Error");
  45. }