Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All you basically need to do is to set the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1" rel="noreferrer"><code>Content-Disposition</code></a> header to <code>attachment</code> to get a 'Save As' dialogue. Here's a kickoff PHP example:</p> <pre><code>&lt;?php header('Content-Type: application/pdf'); header('Content-Disposition: attachment;filename="foo.pdf"'); readfile('/path/to/foo.pdf'); ?&gt; </code></pre> <p>You can't and don't want to do this with Javascript.</p> <p>Important note: due to a poor feature, in MSIE the default filename in 'Save As' dialogue won't be derived from the <code>content-disposition</code> header, it will instead be the last part of the pathinfo in the request URL. To workaround this, append the PDF filename to the link, e.g. <code>http://example.com/pdf/foo.pdf</code>. You can even make use of it in PHP to read the in the pathinfo specified PDF file. Here's a basic example of <code>pdf.php</code>:</p> <pre><code>&lt;?php $file_name = $_SERVER['PATH_INFO']; $file = '/path/to/pdf/files' . $file_name; if (file_exists($file)) { header('Content-Type: application/pdf'); header('Content-Disposition: attachment;filename="' . basename($file_name) . '"'); header('Content-Length: ' . filesize($file)); readfile($file); } else { header('HTTP/1.1 404 Not Found'); } ?&gt; </code></pre> <p>This however assumes that you've <code>MultiViews</code> on so that <code>/pdf/</code> will go through this PHP file, or at least a <code>RewriteRule</code> from <code>/pdf/</code> to <code>/pdf.php/</code>.</p> <p>The major advantage of this approach is that you don't need to change the code whenever you want to add a new PDF file or change the PDF file name.</p> <p>You can even make it more generic by automatically determining and setting the correct content type:</p> <pre><code>&lt;?php $file_name = $_SERVER['PATH_INFO']; $file = '/path/to/all/files' . $file_name; if (file_exists($file)) { header('Content-Type: ' . mime_content_type($file_name)); header('Content-Disposition: attachment;filename="' . basename($file_name) . '"'); header('Content-Length: ' . filesize($file)); readfile($file); } else { header('HTTP/1.1 404 Not Found'); } ?&gt; </code></pre> <p>Name it <code>files.php</code> or so and then you have a generic PHP downloader which you can access by for example <code>http://example.com/files/foo.pdf</code>, <code>http://example.com/files/bar.zip</code>, etcetera.</p> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload