If you want to click on a link and have the browser download a file, just provide a link directly to the file and let the web server and browser deal with it outside of PHP.
If you want PHP to have control over the process (you want to verify a login or something) then something like this:
will instruct PHP to shoot the file out to the browser. The "Content-Disposition" header will cause the browser to pop up a "Save file" dialog, with the filename (which can be different from the name of the script) already populated.
Two gotchas:
You should match the "Content-Type" header to the type of file you're sending. For example, if you're sending a PDF, the content type should read "application/pdf", though "application/octet-stream" will likely work, too.
If your browser knows how to handle the file internally, it may not open the "Save file" dialog at all. For example, if you're sending a text file with a content-type of "text/plain" and a file extension of ".txt", your browser may only display the file. Want the best answers? Ask the best questions:
And I use this code i.e. for CSV file:
$file_type = "csv";
$file_ending = "csv";
header("Content-Type: application/$file_type"
header("Content-Disposition: attachment; filename=invmil.$file_ending"
header("Pragma: no-cache"
header("Expires: 0"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.