HTTP Headers for File Downloads ?
Posted: Sun Nov 15, 2009 1:48 am
I'm getting really confused about the HTTP headers that you need to set before serving a file via PHP. It seems that every book and tutorial has a different way of doing it, so I'm hoping someone out there has done this a million times and can explain why they do it a certain way !
I only have two basic requirements:
1) Be able to serve any type of file, so that the user gets a "Save As..." dialog box (it would be best if they were forced to save it).
2) Prevent the content from being cached anywhere, since eventually this will be paid-for content. (I understand that this doesn't guarantee it)
My code works fine to serve files, but I've seen so many variations of the "Cache-Control", "Pragma", etc... that I've lost the plot.
header("Cache-Control: no-cache");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
readfile($file);
Also, if my "max_execution_time" is set to only 90 seconds in my ini file, will I need to use "set_time_limit($timeout)" for larger files ?
Much thanks in advance !!!
I only have two basic requirements:
1) Be able to serve any type of file, so that the user gets a "Save As..." dialog box (it would be best if they were forced to save it).
2) Prevent the content from being cached anywhere, since eventually this will be paid-for content. (I understand that this doesn't guarantee it)
My code works fine to serve files, but I've seen so many variations of the "Cache-Control", "Pragma", etc... that I've lost the plot.
header("Cache-Control: no-cache");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
readfile($file);
Also, if my "max_execution_time" is set to only 90 seconds in my ini file, will I need to use "set_time_limit($timeout)" for larger files ?
Much thanks in advance !!!