Skip to content
Advertisement

secure and resume support file downloading with php

I want to give permission to some of my website users to download a specific file. the download needs to be resume support just for user convenience.

For that reason it came to my mind to secure the file with this piece of code:

// Check for user session and privileges if it's okay continue
ob_end_clean();
$data = file_get_contents("c:\newdata.pdf");
$showname = "newdata.pdf";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename="".$showname."";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strlen($data));
echo $data;

the problem is that not only it’s not resume support also it’s gonno give my webserver a big overhead and also a lot of memory needed for file_get_contents for large files also for files bigger than 134217728 bytes you will get this error message:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 188862464 bytes)

Any suggestions?

Advertisement

Answer

Check this related question: Is there a good implementation of partial file downloading in PHP?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement