Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z pobieraniem plików
Forum PHP.pl > Forum > PHP
bagsiur
Witam serdecznie.

Mam pewien problem dotyczący pobierania plików z serwera od home.pl. Korzystam ze skryptu który działa poprawnie na innych serwerach, niestety na serwerze od home pliki tak jakby nie pobierały sie do końca w rezultacie czego nie da się ich otworzyć.

Czym może być to spowodowane? Być może ktoś z was zetknął się z podobnym problemem. Może jakaś nieznana mi konfiguracja homa?

Za każda pomoc lub podpowiedź będę bardzo wdzięczny smile.gif Poniżej zamieszczam skrypt:

  1. <?php
  2.  
  3. define('ALLOWED_REFERRER', '');
  4. define('ROOT_DIRECTORY', '.');
  5. define('SEND_BUF_LEN', 1024 * 8);
  6.  
  7. /* ---------------------------------------------------------------------- */
  8.  
  9. if (function_exists('apache_setenv')) @apache_setenv('no-gzip', 1);
  10. @ini_set('zlib.output_compression', 0);
  11. @ini_set('implicit_flush', 1);
  12.  
  13. /* ---------------------------------------------------------------------- */
  14.  
  15. $http_ref = strtoupper(@$_SERVER['HTTP_REFERER']);
  16.  
  17. if (($http_ref !== '') && (ALLOWED_REFERRER !== ''))
  18. {
  19. if (strpos($http_ref, strtoupper(ALLOWED_REFERRER)) === false)
  20. {
  21. header('HTTP/1.1 500 Internal Server Error');
  22. die('Internal server error.');
  23. }
  24. }
  25.  
  26. /* ---------------------------------------------------------------------- */
  27.  
  28. $file = @$_GET['file'];
  29. if (@get_magic_quotes_gpc()) $file = stripslashes($file);
  30.  
  31. $root_dir = realpath(ROOT_DIRECTORY);
  32. $file = realpath($root_dir . '/' . $file);
  33.  
  34. if ((strpos($file, $root_dir) !== 0) || (! is_file($file)))
  35. {
  36. header('HTTP/1.1 404 File Not Found');
  37. die('File not found.');
  38. }
  39.  
  40. /* ---------------------------------------------------------------------- */
  41.  
  42. $fname = basename($file);
  43. $fsize = filesize($file);
  44. $ftime = filemtime($file);
  45.  
  46. $fmime = '';
  47. $range = @$_SERVER['HTTP_RANGE'];
  48.  
  49. $r_start = 0;
  50. $c_length = $fsize;
  51.  
  52. /* ---------------------------------------------------------------------- */
  53.  
  54. if (preg_match('/bytes=([0-9]*)-([0-9]*)/', $range, $tmp))
  55. {
  56. $r_start = (int) $tmp[1];
  57. $r_stop = (int) $tmp[2];
  58. if ($r_stop < $r_start) $r_stop = $fsize - 1;
  59. $c_length = $r_stop - $r_start + 1;
  60.  
  61. header('HTTP/1.1 206 Partial Content');
  62. header('Content-Range: bytes ' .
  63. $r_start . '-' . $r_stop . '/' . $fsize);
  64. }
  65. else
  66. {
  67. header('HTTP/1.1 200 OK');
  68. }
  69.  
  70. /* ---------------------------------------------------------------------- */
  71.  
  72. if (function_exists('mime_content_type'))
  73. {
  74. $fmime = mime_content_type($file);
  75. }
  76. else if (function_exists('finfo_file'))
  77. {
  78. $finfo = finfo_open(FILEINFO_MIME);
  79. $fmime = finfo_file($finfo, $file);
  80. finfo_close($finfo);
  81. }
  82. if ($fmime == '')
  83. {
  84. $fmime = 'application/force-download';
  85. }
  86.  
  87. /* ---------------------------------------------------------------------- */
  88.  
  89. header('Accept-Ranges: bytes');
  90. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $ftime) . ' GMT');
  91. header('Pragma: public');
  92. header('Expires: 0');
  93. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  94. header('Cache-Control: private', false);
  95. header('Content-Description: File Transfer');
  96. header('Content-Disposition: attachment; filename="' . $fname . '"');
  97. header('Content-Type: ' . $fmime);
  98. header('Content-Transfer-Encoding: binary');
  99. header('Content-Length: ' . $c_length);
  100.  
  101. flush();
  102.  
  103. /* ---------------------------------------------------------------------- */
  104.  
  105. if ($fp = @fopen($file, 'rb'))
  106. {
  107. @flock($fp, 1);
  108. @fseek($fp, $r_start);
  109. while ((! feof($fp)) && ($c_length > SEND_BUF_LEN))
  110. {
  111. print(fread($fp, SEND_BUF_LEN));
  112. $c_length = $c_length - SEND_BUF_LEN;
  113. flush();
  114. if (connection_status() != 0) break;
  115. }
  116. if ((! feof($fp)) && (connection_status() == 0))
  117. {
  118. print(fread($fp, $c_length));
  119. flush();
  120. }
  121. @flock($fp, 3);
  122. @fclose($fp);
  123. }
  124.  
  125.  
  126. ?>


Próbowałem też sposobem zawartym w manualu php czyli:

  1. <?php
  2. $file = 'monkey.gif';
  3.  
  4. if (file_exists($file)) {
  5. header('Content-Description: File Transfer');
  6. header('Content-Type: application/octet-stream');
  7. header('Content-Disposition: attachment; filename='.basename($file));
  8. header('Content-Transfer-Encoding: binary');
  9. header('Expires: 0');
  10. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  11. header('Pragma: public');
  12. header('Content-Length: ' . filesize($file));
  13. flush();
  14. readfile($file);
  15. }
  16. ?>


Niestety problem jest identyczny....
darko
Z tego, co pamiętam, to home miał inny nie-Apache'owy serwer, zdaje się, że jakąś odmianę lighttpd czy nginxa, może tutaj leży przyczyna? Usuń tłumienia błędów, może jakiś błąd wywali? Ciężko tu tak na szybko coś wymyślić konkretnego. Spróbuj mozę zwiększyć wartość stałej SEND_BUF_LEN.
Quadina
Na home zamiast exit użyj die(); i wywal Content-Description bo to lighttpd, więc inaczej exit wcale nie przerwie Ci działania, a Content-Description ze spacją w środku będzie oczekiwał na jakieś tam dane (kiedyś sprawdzałem w dokumentacji lighttpd, teraz nie pamiętam szczegółów). Jak nie pójdzie to sprawdź uprawnienia i tak jak wspominał darko wywal @ żeby sprawdzić, czy nie rzuca jakimś błędem po drodze.
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.