How to download a URL and return only the last 20 thousand

I was informed of the Accept-Range header.

I have a url that I call that always returns a 2mb file. I do not need this, and I only need the last section of 20-50k.

I'm not sure how to use this? Do I need to use cURL ? I am currently using file_get_contents() .

Can anyone provide me an example / tutorial?

Thanks.

EDIT: If this is not possible, then what is a message? Here ... EDIT: Ulrika! I am not crazy.

+4
source share
4 answers

This is possible using the Range header if the server supports it. See HTTP 1.1 spec . You want to send the header in the following format in the request:

 Range: bytes=-50000 

This will give you the last 50,000 bytes. Adjust everything you need.

You can specify this header in file_get_contents using context. For instance:

 // Create a stream $opts = array( 'http'=>array( 'method' => "GET", 'header' => "Range: bytes=-50000\r\n" ) ); $context = stream_context_create($opts); // Open the file using the HTTP headers set above $file = file_get_contents('http://www.example.com/', false, $context); 
+3
source

If you were in file_get_contents() and uploaded it to the "cache" passthrough file on disk, you could use unix / linux tail -c to only return the last 20kb or so. This does not reduce the actual transfer, but receives 20 kb per application.

0
source

It's really possible - see this question for an example of sent and received HTTP headers

0
source

you cannot do this. You will need to download the entire file (which will be sent completely, sequentially by the source server) and just drop most of it.

What you are asking is similar to "I'm tuning this radio station on my stereo, and I just want to hear the last 5 minutes of the show, without waiting until everything else finishes or changes the channels."

-4
source

Source: https://habr.com/ru/post/1340934/


All Articles