Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]header i wznawianie pobierania pliku
Forum PHP.pl > Forum > Przedszkole
webmaestro
Moglby ktos podpowiedziec jak zrobic skrypt pobierania pliku przez wyslanie naglowka header z mozliwoscia wznowienia pobrania pliku?
Plik czytam readfile().
erix
Jest taka możliwość. Ale nie przez readfile. Przyda Ci się fopen i fseek.

Ale kluczowe będą tu nagłówki ETag i Content-range.
webmaestro
  1. header('HTTP/1.1 206 Partial Content');
  2. header('Accept-Ranges: bytes');
  3. header('Content-Range: bytes 600-999/1000');
  4. header('Content-Length: 400');

Jak to wyzej dodac do tego niżej??
  1. <?php
  2. header('Content-Type: application/x-unknown');
  3. header('Content-type: application/octet-stream');
  4. header('Content-Disposition: attachment; filename="Daoisoft_HDD_Observer_1.3.2.rar"');
  5. header('Content-Length: 400000');
  6. echo(readfile("http://serwr.pl/plik.rar"));
  7.  
  8.  
  9. ?>
erix
Nie rozumiesz - musisz PRZEPISAĆ ten skrypt tak, aby nie korzystał z readfile, tylko z tych funkcji, o których wspomniałem. Ew. skorzystać ze stream_context, ale zrób tak, jak napisałem.
webmaestro
  1. $url="http://www.www.pl/plik.rar";
  2. $httphandle = fopen($url,"r");
  3.  
  4. header('Content-Type: application/x-unknown');
  5.  
  6. header('Content-type: application/octet-stream');
  7.  
  8. header('Content-Disposition: attachment; filename="nazwa.rar"');
  9.  
  10. header('Content-Length: 400000');
  11. fpassthru($httphandle);
  12. fclose($httphandle);


Cos w ten styl?
erix
A skąd skrypt ma wiedzieć, od którego offsetu wznowić? Przecież nie będzie wysyłał całości od początku.
webmaestro
No ja wlasnie nie wiem jak to dziala i prosze was o jakis przyklad...

pomoze ktos??
erix
Z łaski swojej, poszukaj konstrukcji nagłówka Content-range, to będziesz wiedział, co do czego.

Gotowce, to nie ten dział.
webmaestro
Cytat(erix @ 30.12.2009, 14:41:37 ) *
Z łaski swojej, poszukaj konstrukcji nagłówka Content-range, to będziesz wiedział, co do czego.

Gotowce, to nie ten dział.

  1. <?
  2.  
  3. header('Content-Disposition: attachment;');
  4. $ch = curl_init();
  5. curl_setopt($ch, CURLOPT_URL,"http://www.strona.pl/plik.rar");
  6. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  7. curl_exec($ch);
  8. curl_close($ch);
  9.  
  10. ?>


A curlem da to sie zrobic??
erix
Czy ogniem możesz zgasić wodę?

Nie, nie da się; cURL jest do czego innego - rusz swoje szanowne cztery litery i poszukaj, jak wygląda, jak jest skonstruowany ten nagłówek, o którym masz już wspomniane n-ty raz.

Gotowca nie dostaniesz, na to nie licz.
webmaestro
Cytat(erix @ 30.12.2009, 15:53:35 ) *
Czy ogniem możesz zgasić wodę?

Nie, nie da się; cURL jest do czego innego - rusz swoje szanowne cztery litery i poszukaj, jak wygląda, jak jest skonstruowany ten nagłówek, o którym masz już wspomniane n-ty raz.

Gotowca nie dostaniesz, na to nie licz.

A no sie myslisz bo curlem sie da zrobic tez...
Poczytaj o: CURLOPT_RANGE


  1. <?
  2. $file = "sa.rar";
  3. $filesize = filesize($file);
  4. $fileName= "sa.rar";
  5. $offset = 0;
  6. $length = $filesize;
  7.  
  8. if ( isset($_SERVER['HTTP_RANGE']) ) {
  9. // if the HTTP_RANGE header is set we're dealing with partial content
  10.  
  11. $partialContent = true;
  12.  
  13. // find the requested range
  14. // this might be too simplistic, apparently the client can request
  15. // multiple ranges, which can become pretty complex, so ignore it for now
  16. preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
  17.  
  18. $offset = intval($matches[1]);
  19. $length = intval($matches[2]) - $offset;
  20. } else {
  21. $partialContent = false;
  22. }
  23.  
  24. $file = fopen($file, 'r');
  25.  
  26. // seek to the requested offset, this is 0 if it's not a partial content request
  27. fseek($file, $offset);
  28.  
  29. $data = fread($file, $length);
  30.  
  31. fclose($file);
  32.  
  33. if ( $partialContent ) {
  34. // output the right headers for partial content
  35.  
  36. header('HTTP/1.1 206 Partial Content');
  37.  
  38. header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
  39. }
  40.  
  41. // output the regular HTTP headers
  42. header("Content-Type: application/octet-stream");
  43. header('Content-Length: ' . $filesize);
  44. header('Content-Disposition: attachment; filename="' . $fileName . '"');
  45. header('Accept-Ranges: bytes');
  46.  
  47. // don't forget to send the data too
  48. print($data);


A w tym kodzie co moze byc zle ze nie da sie zapauzowac/wznowic pobierania?questionmark.gif
erix
Skoro twierdzisz, że da się cURLem, to zrób.

Ale zwrócę Ci tylko uwagę na malutki szczegół:
Cytat
Range(s) of data to retrieve in the format "X-Y" where X or Y are optional. HTTP transfers also support several intervals, separated with commas in the format "X-Y,N-M".


Jest różnica między send a retrieve.
webmaestro
  1. curl_setopt($ch, CURLOPT_RANGE, "0-4096");


A to do czego słuzy??
webmaestro
  1. $file="plik.rar";
  2.  
  3.  
  4. $filename = "plik.rar";
  5. $size="4555455";// waga w byte
  6. header("Cache-Control:");
  7. header("Cache-Control: public");
  8. header("Content-Type: application/octet-stream");
  9. if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
  10. {
  11. $iefilename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
  12. header("Content-Disposition: attachment; filename=\"$iefilename\"");
  13. } else
  14. {
  15. header("Content-Disposition: attachment; filename=\"$filename\"");
  16. }
  17. header("Accept-Ranges: bytes");
  18. if(isset($_SERVER['HTTP_RANGE']))
  19. {
  20. list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
  21. str_replace($range, "-", $range);
  22. $size2=$size-1;
  23. $new_length=$size2-$range;
  24. header("HTTP/1.1 206 Partial Content");
  25. header("Content-Length: $new_length");
  26. header("Content-Range: bytes $range$size2/$size");
  27. }
  28. else
  29. {
  30. $size2=$size-1;
  31. header("Content-Range: bytes 0-$size2/$size");
  32. header("Content-Length: ".$size);
  33. }
  34. $fp=fopen("$file","r");
  35. fseek($fp,$range);
  36. while(!feof($fp))
  37. {
  38. print(fread($fp,1024*8));
  39. }
  40. fclose($fp);
  41.  
  42.  


Takie cos zrobielm, i teraz gdy pobieram plik rar to psuje mi sie kawalek archiwum. Co moze byc przyczyna??
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.