1

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?

EBAG
  • 21,625
  • 14
  • 59
  • 93
  • Only `application/octet-stream` is the official media type for binary data that’s supposed to be downloaded by the client. – Gumbo Sep 22 '09 at 13:04
  • possible duplicate of [Is there a good implementation of partial file downloading in PHP?](http://stackoverflow.com/questions/1395656/is-there-a-good-implementation-of-partial-file-downloading-in-php) – Sindre Sorhus Apr 04 '13 at 11:31

2 Answers2

1

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

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Adam Wright
  • 48,938
  • 12
  • 131
  • 152
1

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

Community
  • 1
  • 1
Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64