upload.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. $files = $_FILES['file'];
  3. $file_path = $files['tmp_name'][0]; // temporary upload path of the first file
  4. $file_name = $_POST['name']; // desired name of the file
  5. $uploaded = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/public/uploads/' . $_FILES['file']['name']);
  6. if($uploaded) {
  7. header('Access-Control-Allow-Origin: *');
  8. header('Content-type: application/json');
  9. $data = ['url' => '/public/uploads/' . basename($file_name), 'message' => 'The file ' . $file_name . ' has been uploaded.'];
  10. http_response_code(201);
  11. echo json_encode($data);
  12. }
  13. /*
  14. die;
  15. ini_set('display_errors', 1);
  16. ini_set('display_startup_errors', 1);
  17. error_reporting(E_ALL);
  18. // Get the maximum upload file size
  19. $max_size = ini_get('upload_max_filesize');
  20. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  21. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
  22. header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
  23. }
  24. //Make sure you remove those you do not want to support
  25. header('Access-Control-Allow-Origin: *');
  26. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
  27. header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  28. }
  29. //Just exit with 200 OK with the above headers for OPTIONS method
  30. exit(0);
  31. }
  32. $target_dir = __DIR__ . DIRECTORY_SEPARATOR . 'uploads';
  33. $file_name = basename($_FILES['file']['name']);
  34. $file_size = $_FILES['file']['size'];
  35. $target_file = $target_dir . DIRECTORY_SEPARATOR . $file_name;
  36. // Validate file size
  37. if ($file_size > $max_size) {
  38. header('Access-Control-Allow-Origin: *');
  39. header('Content-type: application/json');
  40. $data = ['message' => 'File size exceeds the maximum allowed size of ' . $max_size . '.'];
  41. http_response_code(400);
  42. echo json_encode($data);
  43. exit;
  44. }
  45. // Sanitize file name to prevent directory traversal attacks
  46. $file_name = preg_replace('/[^a-zA-Z0-9._-]/', '', $file_name);
  47. $target_file = $target_dir . DIRECTORY_SEPARATOR . $file_name;
  48. try {
  49. if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
  50. header('Access-Control-Allow-Origin: *');
  51. header('Content-type: application/json');
  52. $data = ['url' => $target_file, 'message' => 'The file ' . $file_name . ' has been uploaded.'];
  53. http_response_code(201);
  54. echo json_encode($data);
  55. } else {
  56. throw new Exception('Unable to move the uploaded file to its final location:' . $target_file);
  57. }
  58. } catch (\Throwable $th) {
  59. header('Access-Control-Allow-Origin: *');
  60. header('Content-type: application/json');
  61. $data = ['message' => 'Sorry, there was an error uploading your file.', 'error' => $th->getMessage()];
  62. http_response_code(400);
  63. echo json_encode($data);
  64. }
  65. */