tags:

views:

162

answers:

2

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?

+1  A: 

Use readfile($fileName) to pass the file data straight to the client.

Edit: If you want resume support, you'll have to write a version of readfile that allows you to specify a starting byte. This is easily done with fopen/fread. There is no need to read everything into memory - you just read a chunk, send it, read another, send it…etc

Adam Wright
this method is not resume support
EBAGHAKI
For you edit: it's not that simple\
EBAGHAKI
+1  A: 

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

Simon Groenewolt