upload_handler.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. include '../auth.php';
  3. ?>
  4. <?php
  5. header('Access-Control-Allow-Origin: *');
  6. header('Cache-Control: no-cache');
  7. header('Access-Control-Request-Headers: *');
  8. header('Access-Control-Allow-Headers: Content-Type');
  9. $ds = DIRECTORY_SEPARATOR;
  10. $dataarr = [];
  11. //Set storefolder to uploads/username
  12. if(isset($_POST['targetModule']) !== False){
  13. if (isset($_POST['extmode']) && $_POST['extmode'] != ""){
  14. //request upload to external storage devices
  15. $storeFolder = $_POST['extmode'] . "/" . $_POST['targetModule'] . "/";
  16. //
  17. }else{
  18. //Use default module folder instead
  19. $storeFolder = "../" . $_POST['targetModule'] . "/uploads/";
  20. }
  21. }else{
  22. echo 'Unset target Module.';
  23. http_response_code(404);
  24. die();
  25. }
  26. if(isset($_POST['filetype']) !== False && $_POST['filetype'] != ""){
  27. $rawformat = strtolower($_POST['filetype']);
  28. $allowtype = explode(",",$rawformat);
  29. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  30. if (!in_array(strtolower($ext),$allowtype)){
  31. echo 'This format is not supported.';
  32. http_response_code(404);
  33. die();
  34. }
  35. }
  36. if (!empty($_FILES)) {
  37. $tempFile = $_FILES['file']['tmp_name'];
  38. if (strpos($storeFolder,"/media/") === 0){
  39. //Upload to external USB devices
  40. if (file_exists($storeFolder) == false){
  41. mkdir($storeFolder,0777);
  42. }
  43. $targetPath = $storeFolder;
  44. }else{
  45. //Uplaod to internal storage path
  46. $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
  47. }
  48. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  49. //$ext = strtolower($ext)
  50. $filename = str_replace("." . $ext,"",$_FILES['file']['name']);
  51. $targetFile = $targetPath. "inith" . bin2hex($filename).".".strtolower($ext);
  52. //$targetFile = $targetPath. "testt";
  53. move_uploaded_file($tempFile,$targetFile);
  54. header('Location: ' . "../" .$_POST['targetModule']);
  55. }else{
  56. echo 'Unknown Error.';
  57. http_response_code(404);
  58. die();
  59. }
  60. ?>