Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Upload pliku w tinyMCE
Forum PHP.pl > Forum > Przedszkole
casperii
Witam panowie, czy moglibyście spojrzeć w kod (zaznaczam, że nie jest mojego autorstwa). Zwraca błąd 500.
oczywiście pozmieniane w tablicy $accepted_origins na mój host. $imageFolder również wskazany na odpowiedni

Problem pojawia się w tym miejscu:

  1. reset ($_FILES);
  2. $temp = current($_FILES);
  3.  
  4. if(is_uploaded_file($temp['tmp_name'])){



całość kodu php:

  1. <?php
  2.  
  3. // Only these origins are allowed to upload images
  4. $accepted_origins = array("http://localhost", "https://www.codexworld.com", "http://192.168.1.1", "http://example.com");
  5.  
  6. // Set the upload folder
  7. $imageFolder = "uploads/";
  8.  
  9. if (isset($_SERVER['HTTP_ORIGIN'])) {
  10. // same-origin requests won't set an origin. If the origin is set, it must be valid.
  11. if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
  12. header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
  13. } else {
  14. header("HTTP/1.1 403 Origin Denied");
  15. return;
  16. }
  17. }
  18.  
  19. // Don't attempt to process the upload on an OPTIONS request
  20. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  21. header("Access-Control-Allow-Methods: POST, OPTIONS");
  22. return;
  23. }
  24.  
  25. reset ($_FILES);
  26. $temp = current($_FILES);
  27. if (is_uploaded_file($temp['tmp_name'])){
  28. /*
  29.   If your script needs to receive cookies, set images_upload_credentials : true in
  30.   the configuration and enable the following two headers.
  31.   */
  32. // header('Access-Control-Allow-Credentials: true');
  33. // header('P3P: CP="There is no P3P policy."');
  34.  
  35. // Sanitize input
  36. if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
  37. header("HTTP/1.1 400 Invalid file name.");
  38. return;
  39. }
  40.  
  41. // Verify extension
  42. if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "jpeg", "png"))) {
  43. header("HTTP/1.1 400 Invalid extension.");
  44. return;
  45. }
  46.  
  47. // Accept upload if there was no origin, or if it is an accepted origin
  48. $filetowrite = $imageFolder . $temp['name'];
  49. if(move_uploaded_file($temp['tmp_name'], $filetowrite)){
  50. // Determine the base URL
  51. $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https://" : "http://";
  52. $baseurl = $protocol . $_SERVER["HTTP_HOST"] . rtrim(dirname($_SERVER['REQUEST_URI']), "/") . "/";
  53.  
  54. // Respond to the successful upload with JSON.
  55. // Use a location key to specify the path to the saved image resource.
  56. // { location : '/your/uploaded/image/file'}
  57. echo json_encode(array('location' => $baseurl . $filetowrite));
  58. }else{
  59. header("HTTP/1.1 400 Upload failed.");
  60. return;
  61. }
  62. } else {
  63. // Notify editor that the upload failed
  64. header("HTTP/1.1 500 Server Error");
  65. }
  66.  
  67. ?>


Źródło kodu: link
trueblue
A nie dostrzegasz, że błąd 500 jest zwracany kiedy nie jest spełniony warunek:
  1. is_uploaded_file($temp['tmp_name'])

?
casperii
@trueblue przecież masz napisane, że co powoduje błąd 500.
trueblue
Aaa, dzięki. To będę wiedział co poprawić.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.