Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]header i wznawianie pobierania pliku
webmaestro
post
Post #1





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


Moglby ktos podpowiedziec jak zrobic skrypt pobierania pliku przez wyslanie naglowka header z mozliwoscia wznowienia pobrania pliku?
Plik czytam readfile().
Go to the top of the page
+Quote Post
erix
post
Post #2





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




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.
Go to the top of the page
+Quote Post
zegarek84
post
Post #3





Grupa: Zarejestrowani
Postów: 1 332
Pomógł: 294
Dołączył: 12.10.2008
Skąd: Olkusz

Ostrzeżenie: (0%)
-----


Pobieranie pliku przez skrypt PHP z możliwością wznowienia przerwanego połączenia
Go to the top of the page
+Quote Post
webmaestro
post
Post #4





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


  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. ?>
Go to the top of the page
+Quote Post
erix
post
Post #5





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




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.
Go to the top of the page
+Quote Post
webmaestro
post
Post #6





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


  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?

Ten post edytował webmaestro 29.12.2009, 23:45:42
Go to the top of the page
+Quote Post
erix
post
Post #7





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




A skąd skrypt ma wiedzieć, od którego offsetu wznowić? Przecież nie będzie wysyłał całości od początku.
Go to the top of the page
+Quote Post
webmaestro
post
Post #8





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


No ja wlasnie nie wiem jak to dziala i prosze was o jakis przyklad...

pomoze ktos??
Go to the top of the page
+Quote Post
erix
post
Post #9





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




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

Gotowce, to nie ten dział.
Go to the top of the page
+Quote Post
webmaestro
post
Post #10





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


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??
Go to the top of the page
+Quote Post
erix
post
Post #11





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




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.
Go to the top of the page
+Quote Post
webmaestro
post
Post #12





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


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?(IMG:style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
erix
post
Post #13





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




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.
Go to the top of the page
+Quote Post
webmaestro
post
Post #14





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


  1. curl_setopt($ch, CURLOPT_RANGE, "0-4096");


A to do czego słuzy??
Go to the top of the page
+Quote Post
erix
post
Post #15





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




http://megaslownik.pl/slownik/angielsko_po...201382,retrieve

(IMG:style_emoticons/default/sciana.gif)
Go to the top of the page
+Quote Post
webmaestro
post
Post #16





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 25.12.2009

Ostrzeżenie: (0%)
-----


  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??
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 03:52