1
votes

Trying to get the entire file downloaded -- but I assume the script is exiting with the first chunksize pass in the stream.

What needs to be coded in to get full file size download?

$image_link=$_GET['image_link'];
$fullPathDL=$path0.$image_link;
$fsize = filesize($fullPathDL);
$path_parts = pathinfo($fullPathDL);
if ($fd = fopen($fullPathDL, "rb")) {
    $fsize = filesize($fullPathDL);
    header("Content-type: application/jpg"); // add here more headers for diff. extensions
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
    header("Content-length: $fsize");
    //    header("Cache-control: private"); //use this to open files directly
        while(!feof($fd)) {

        $buffer = fread($fd, 8192); 

        echo $buffer;
    }

fclose ($fd);
exit;

}

1
What exactly is happening? I don't understand.Unicron
What chunks are you talking about? You aren't asking for chunked transfer encoding, and you aren't creating syntactically-correct chunks for chunked transfer encoding either...Charles

1 Answers

2
votes

The reason why your example doesn't work is the entirely present in the buffer. It's been awhile since I've looked at the standard, but at least I can give you a step in the right direction. Basically, when reading from chunked-transfer, the first couple bytes I believe represent the number of bytes in the chunk. Something like this (taken from wikipedia).

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

You basically need to check the number of bytes to read, read the bytes, and repeat until the number of bytes to read is 0.

The easiest way to accomplish this is to either specify in your request headers HTTP 1.0 (which does not support chunked transfer), or use a library which handles this for you, i.e., CURL.