Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Wznawianie pobierania plików
Forum PHP.pl > Forum > PHP
radmistrz2
Poniższy skrypt pozwala (teoretycznie) wznawiać i pauzować pobieranie plików. Niestety nie działa tak jak chce. W firefoxie nie wznawia mi pobierania i nie pokazuje rozmiaru pliku. Zkolei we flashgecie pokazuje rozmiar pliku i go wznawia, lecz pokazuje błąd CRC w pliku - wydaje mi się że po każdym wznowieniu wysyła początek pliku. Poradzicie coś na to??

  1. <?php
  2. function getSizeFile($url) {
  3. if (substr($url,0,4)=='http') {
  4. $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
  5. if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; }
  6. else { $x = $x['content-length']; }
  7. }
  8. else { $x = @filesize($url); }
  9.  
  10. return $x;
  11. }
  12.  
  13. function output_file($file, $name, $mime_type='')
  14. {
  15. /*
  16.  This function takes a path to a file to output ($file),
  17.  the filename that the browser will see ($name) and
  18.  the MIME type of the file ($mime_type, optional).
  19.  
  20.  If you want to do something on download abort/finish,
  21.  register_shutdown_function('function_name');
  22.  */
  23.  
  24. $size = getSizeFile($file);
  25. $name = rawurldecode($name);
  26.  
  27. /* Figure out the MIME type (if not specified) */
  28. $known_mime_types=array(
  29. "pdf" => "application/pdf",
  30. "txt" => "text/plain",
  31. "html" => "text/html",
  32. "htm" => "text/html",
  33. "exe" => "application/octet-stream",
  34. "zip" => "application/zip",
  35. "doc" => "application/msword",
  36. "xls" => "application/vnd.ms-excel",
  37. "ppt" => "application/vnd.ms-powerpoint",
  38. "gif" => "image/gif",
  39. "png" => "image/png",
  40. "jpeg"=> "image/jpg",
  41. "jpg" => "image/jpg",
  42. "php" => "text/plain"
  43. );
  44.  
  45. if($mime_type==''){
  46. $file_extension = strtolower(substr(strrchr($file,"."),1));
  47. if(array_key_exists($file_extension, $known_mime_types)){
  48. $mime_type=$known_mime_types[$file_extension];
  49. } else {
  50. $mime_type="application/force-download";
  51. };
  52. };
  53.  
  54. @ob_end_clean(); //turn off output buffering to decrease cpu usage
  55.  
  56. // required for IE, otherwise Content-Disposition may be ignored
  57. if(ini_get('zlib.output_compression'))
  58. ini_set('zlib.output_compression', 'Off');
  59.  
  60. header('Content-Type: ' . $mime_type);
  61. header('Content-Disposition: attachment; filename="'.$name.'"');
  62. header("Content-Transfer-Encoding: binary");
  63. header('Accept-Ranges: bytes');
  64.  
  65. /* The three lines below basically make the
  66.   download non-cacheable */
  67. header("Cache-control: private");
  68. header('Pragma: private');
  69. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  70.  
  71. // multipart-download and download resuming support
  72. if(isset($_SERVER['HTTP_RANGE']))
  73. {
  74. list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
  75. list($range) = explode(",",$range,2);
  76. list($range, $range_end) = explode("-", $range);
  77. $range=intval($range);
  78. if(!$range_end) {
  79. $range_end=$size-1;
  80. } else {
  81. $range_end=intval($range_end);
  82. }
  83.  
  84. $new_length = $range_end-$range+1;
  85. header("HTTP/1.1 206 Partial Content");
  86. header("Content-Length: $new_length");
  87. header("Content-Range: bytes $range-$range_end/$size");
  88. } else {
  89. $new_length=$size;
  90. header("Content-Length: ".$size);
  91. }
  92.  
  93. /* output the file itself */
  94. $chunksize = 1*(1024*1024); //you may want to change this
  95. $bytes_send = 0;
  96. if ($file = fopen($file, 'r'))
  97. {
  98. if(isset($_SERVER['HTTP_RANGE']))
  99. fseek($file, $range);
  100.  
  101. while(!feof($file) &&
  102. ($bytes_send<$new_length)
  103. )
  104. {
  105. $buffer = fread($file, $chunksize);
  106. print($buffer); //echo($buffer); // is also possible
  107. flush();
  108. $bytes_send += strlen($buffer);
  109. }
  110. fclose($file);
  111. } else die('Error - can not open file.');
  112.  
  113. die();
  114. }
  115.  
  116. /*********************************************
  117.   Example of use
  118. **********************************************/
  119.  
  120. /*
  121. Make sure script execution doesn't time out.
  122. Set maximum execution time in seconds (0 means no limit).
  123. */
  124. $file_path='that_one_file.txt';
  125. output_file('http://www.cos.pl/plik.rar', 'plik.rar', 'application/zip');
  126.  
  127. ?>
erix
Przeanalizuj, jakie nagłówki faktycznie nagłówki lecą, to raz.

Dwa - utwórz jakiś plik z kolejnymi wartościami i przeanalizuj, w którym momencie pojawia się nieścisłość.
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.