How to Force the Download of a File with HTTP Headers and PHP

It’s quite a common scenario with the web to want to force a file to download, instead of allowing the browser to open it. This can apply to images, pdfs, html, anything a web browser can open (which is more and more these days). To accomplish this, we need to set some http response headers: Content-Type: application/octet-stream Content-Disposition: attachment; filename=”test.txt” Within PHP was can do this with a function like: function forceDownload($filename, $type = “application/octet-stream”) { header(‘Content-Type: ‘.$type.’; charset=utf-8′); header(‘Content-Disposition: … Continued