Hello Mates,
I am using the following code to download a pdf file of size 3MB.
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
$file="test.pdf";
ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
ob_end_flush();
set_time_limit(0);
//readfile($file);
readfile_chunked($file);
exit;
I put in all available suggestions I got in order to make it faster and hence all the above extra code. But still it is taking too much time to show the pdf on browser. Any suggestion would be great help. Thank you.