Question

I am programming a service that has to force the download of a file.

I know the possibility of setting the HTTP-headers using PHP and then sending the file using the readfile-function. But I think this is not a good way for sending larger files because it would need a lot of server performance and the maximum execution time of the PHP-scripts would be exceeded.

So is it possible to send the HTTP-headers using PHP (I have to modify them depending on entries in a mysql-database.) and then let Apache send the file body? I have to add that I could also use perl scripts but I also do not see a possibility for doing this in a cgi-script. Thanks.

Was it helpful?

Solution 3

I found this link: http://jasny.net/articles/how-i-php-x-sendfile/. X-Sendfile is a module which seems to allow exactly what I want to do. It redirects the body of the HTTP-response and will Apache let handle everything. I just installed and tried it and it works fine.

OTHER TIPS

You can do this strictly with apache if the location and/or filetype of the download is known ahead of time:

<Location /downloads>
    SetEnvIf Request_URI ".attachment-extension$" FILENAME=$0
    Header set "Content-disposition" "attachment; filename=%{FILENAME}"
</Location>

because it would need a lot of server performance

I don't believe this is the case. As long as sending the file to the client is the last thing the script does before it terminates, the difference in CPU/RAM performance between sending the file from PHP and letting Apache handle it directly should be minimal, if there is one at all.

Unless you have a very large number of Gbps bandwidth on your server with an incredibly fast HDD setup, you would run into a bandwidth problem long before you ran into a server system resources problem.

Admittedly this discussion is based largely on conjecture (since I know nothing about your hosting setup), so YMMV.

the maximum execution time of the PHP-scripts would be exceeded

So just call set_time_limit(0);. That what it's for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top