upload_handler.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. //declare the PHP_VERSION_ID
  52. if (!defined('PHP_VERSION_ID')) {
  53. $version = explode('.', PHP_VERSION);
  54. define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
  55. }
  56. //check if PHP was higher than 7.4, if true then not using inith filename
  57. if(PHP_VERSION_ID >= 70404){
  58. $targetFile = $targetPath.$filename.".".strtolower($ext);
  59. }else{
  60. $targetFile = $targetPath. "inith" . bin2hex($filename).".".strtolower($ext);
  61. }
  62. //$targetFile = $targetPath. "testt";
  63. move_uploaded_file($tempFile,$targetFile);
  64. header('Location: ' . "../" .$_POST['targetModule']);
  65. }else{
  66. echo 'Unknown Error.';
  67. http_response_code(404);
  68. die();
  69. }
  70. ?>