php: Change name / redirect a download

Greg2FS used Ask the Experts™
Hello, I have a file on my server which requires to be downloaded with different name depending of the username:
-paul must download
-jim must download

but they all download the same

I currently does that:
header("Content-Transfer-Encoding: binary");
header("Content-Type: application/octet-stream");
header('Content-Length: ' . filesize(""));
header("Content-Disposition: attachment;");

Open in new window


But it seems than php can reach it's time limit this way... Is defining a big time limit, the only way to solve that problem ? Can't memory problem appen ?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

header("Content-Disposition: attachment;"); 

should be 

$username = "paul"; // or whatever the name is.. 
header("Content-Disposition: attachment; filename=$");

Open in new window


sorry just a typing mistake...
Just before sending the header, echo the file name and see if the user name is populated properly. (Once you put echo, header will not work. This is just to make sure that the username is populated properly.)


I think I didn't express myself well: it works, but if I use that for a big file like 500Mo it just stop downoading after some time, then I had the idea of increasing the php execution time limit and it works, but
1: isn't a sort of security hole to put an infinite time limit to allow slow internet connections to download the file ?
2: how memory is used this way ? for example if many people download the same time, won't ram be saturated ?
3: isn't there a better way to allow resuming a download, using several section...
Oh sorry.. I misunderstood your question

1. It isn't a security hole.. but if a script takes a lot of time, it can become burden for the server.
2. You can limit the number of simultaneous connections
3. Certainly, if you split the files into pieces, you can download all of them separately. See some discussion regarding this here:


Using fread is a good solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial