How to force file download with PHP

I want to require a file to be downloaded upon the user visiting a web page with PHP. I think it has something to do with file_get_contents, but am not sure how to execute it. After downloading a file with header(location) it is not redirecting to another page. It just stops.

Download File Using JavaScript/jQuery

Use an invisible <iframe>: To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file’s MIME Type to a nonsensical value, such as application/x-please-download-me or alternatively application/octet-stream, which is used for arbitrary binary data. If you only want to open it in a … Read more

Download File Using JavaScript/jQuery

Use an invisible <iframe>: To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file’s MIME Type to a nonsensical value, such as application/x-please-download-me or alternatively application/octet-stream, which is used for arbitrary binary data. If you only want to open it in a … Read more

Do I need Content-Type: application/octet-stream for file download?

No. The content-type should be whatever it is known to be, if you know it. application/octet-stream is defined as “arbitrary binary data” in RFC 2046, and there’s a definite overlap here of it being appropriate for entities whose sole intended purpose is to be saved to disk, and from that point on be outside of … Read more