php: Change name / redirect a download

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

but they all download the same file.zip

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

Open in new window

readfile("file.zip");

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 ?

Thanks
Comment
Watch Question

Do more with

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

header("Content-Disposition: attachment; filename=username.zip"); 

should be 

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

Open in new window

Author

Commented:
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.)

Author

Commented:
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: http://php.net/manual/en/function.fread.php

Author

Commented:
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