<p>What code do you need to add in PHP to automatically have the browser download a file to the local machine when a link is visited?</p>

<p>I am specifically thinking of functionality similar to that of download sites that prompt the user to save a file to disk once you click on the name of the software?</p>

[{'answer_id': 40945, 'author': 'grom', 'author_id': 486, 'author_profile': 'https://Stackoverflow.com/users/486', 'pm_score': 5, 'selected': False, 'text': '<p>Here is an example of sending back a pdf.</p>\n\n<pre><code>header(\'Content-type: application/pdf\');\nheader(\'Content-Disposition: attachment; filename="\' . basename($filename) . \'"\');\nheader(\'Content-Transfer-Encoding: binary\');\nreadfile($filename);\n</code></pre>\n\n<p><a href="https://stackoverflow.com/questions/40943/how-to-automatically-start-a-download-in-php#40947">@Swish</a> I didn\'t find application/force-download content type to do anything different (tested in IE and Firefox). Is there a reason for not sending back the actual MIME type?</p>\n\n<p>Also in the PHP manual <a href="http://au.php.net/manual/en/function.header.php#71993" rel="noreferrer">Hayley Watson</a> posted:</p>\n\n<blockquote>\n  <p>If you wish to force a file to be downloaded and saved, instead of being rendered, remember that there is no such MIME type as "application/force-download". The correct type to use in this situation is "application/octet-stream", and using anything else is merely relying on the fact that clients are supposed to ignore unrecognised MIME types and use "application/octet-stream" instead (reference: Sections 4.1.4 and 4.5.1 of RFC 2046).</p>\n</blockquote>\n\n<p>Also according <a href="http://www.iana.org/assignments/media-types/application/" rel="noreferrer">IANA</a> there is no registered application/force-download type.</p>\n'}, {'answer_id': 40947, 'author': 'Robert Swisher', 'author_id': 1852, 'author_profile': 'https://Stackoverflow.com/users/1852', 'pm_score': 7, 'selected': True, 'text': '<p>Send the following headers before outputting the file:</p>\n\n<pre><code>header("Content-Disposition: attachment; filename=\\"" . basename($File) . "\\"");\nheader("Content-Type: application/octet-stream");\nheader("Content-Length: " . filesize($File));\nheader("Connection: close");\n</code></pre>\n\n<p><a href="https://stackoverflow.com/users/486/grom">@grom</a>: Interesting about the \'application/octet-stream\' MIME type. I wasn\'t aware of that, have always just used \'application/force-download\' :)</p>\n'}, {'answer_id': 8349390, 'author': 'vdbuilder', 'author_id': 1076318, 'author_profile': 'https://Stackoverflow.com/users/1076318', 'pm_score': 4, 'selected': False, 'text': '<p>A clean example.</p>\n\n<pre><code>&lt;?php\n    header(\'Content-Type: application/download\');\n    header(\'Content-Disposition: attachment; filename="example.txt"\');\n    header("Content-Length: " . filesize("example.txt"));\n\n    $fp = fopen("example.txt", "r");\n    fpassthru($fp);\n    fclose($fp);\n?&gt;\n</code></pre>\n'}, {'answer_id': 10014110, 'author': 'Omidoo', 'author_id': 879163, 'author_profile': 'https://Stackoverflow.com/users/879163', 'pm_score': 0, 'selected': False, 'text': '<p>my code works for txt,doc,docx,pdf,ppt,pptx,jpg,png,zip extensions and I think its better to use the actual MIME types explicitly.</p>\n\n<pre><code>$file_name = "a.txt";\n\n// extracting the extension:\n$ext = substr($file_name, strpos($file_name,\'.\')+1);\n\nheader(\'Content-disposition: attachment; filename=\'.$file_name);\n\nif(strtolower($ext) == "txt")\n{\n    header(\'Content-type: text/plain\'); // works for txt only\n}\nelse\n{\n    header(\'Content-type: application/\'.$ext); // works for all extensions except txt\n}\nreadfile($decrypted_file_path);\n</code></pre>\n'}, {'answer_id': 68258678, 'author': 'gtamborero', 'author_id': 3577257, 'author_profile': 'https://Stackoverflow.com/users/3577257', 'pm_score': 1, 'selected': False, 'text': '<p>None of above worked for me!</p>\n<p>Working on 2021 for WordPress and PHP:</p>\n<pre><code>&lt;?php\n$file = ABSPATH . \'pdf.pdf\'; // Where ABSPATH is the absolute server path, not url\n//echo $file; //Be sure you are echoing the absolute path and file name\n$filename = \'Custom file name for the.pdf\'; /* Note: Always use .pdf at the end. */\n\nheader(\'Content-type: application/pdf\');\nheader(\'Content-Disposition: inline; filename=&quot;\' . $filename . \'&quot;\');\nheader(\'Content-Transfer-Encoding: binary\');\nheader(\'Content-Length: \' . filesize($file));\nheader(\'Accept-Ranges: bytes\');\n@readfile($file);\n</code></pre>\n<p>Thanks to: <a href="https://qastack.mx/programming/4679756/show-a-pdf-files-in-users-browser-via-php-perl" rel="nofollow noreferrer">https://qastack.mx/programming/4679756/show-a-pdf-files-in-users-browser-via-php-perl</a></p>\n'}]
['https://Stackoverflow.com/questions/40943', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/277/']