I would like to stream a file from S3 to the client browser using PHP. I could manage to do it but I have a problem with large files. First, it takes all the memory available for the session and end up with a "Memory exhausted" error. Second, when reaching "fread(...)" for the first time, it hangs almost 1 min for a 50Mo file before to start sending to the browser. How can I handle these 2 problems? Here is my code.
$client->register_stream_wrapper();
if($stream = fopen('s3://' . S3_BUCKET . '/' . $destination_s3, 'r'))
{
//Send file to browser.
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-control: no-store");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Type: " . $file_type);
while (!feof($stream)) {
echo fread($stream, 1024);
flush();
}
fclose($stream);
}