Reference number: CH001113 Make link prompt visitor to download .PDF, .DOC, or other file.
Question:
Make link prompt visitor to download .PDF, .DOC, or other file.
Answer:
Note: This document is for Webmasters who want .PDF, .DOC, or other file links to open a specific way on their web page. If you want to change the file settings in your browser see document CH001114. In some situations when you're creating a web page with links to an Adobe Acrobat .PDF, Microsoft Word. DOC, Microsoft Excel .XLS, or other external program file, you may want the Internet browser to prompt the user to download the file instead of opening it in the browser window or in the external program. There are a few different methods you can do this. 1. We recommend on the web page that the user right-click the link and choose the option to Save or Save as the file. 2. Save the file within a compressed format such as a .ZIP file. Information about compressing a file can be found on document CH000808. 3. Create the below PHP file that can be used to open
.PDF files and if modified, .DOC or other files. 3a. Start by creating a new file on your server or on your computer called download.php 3b. Once this file has been created copy and paste the below code into the file.
<?php if (isset($_GET['file'])) { $file = $_GET['file']; if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) { header('Content-type: application/pdf'); header("Content-Disposition: attachment; filename=\"$file\""); readfile($file); } } else { header("HTTP/1.0 404 Not Found"); echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>"; } ?>
|
3c. Once the above has been done, save the file and if needed upload to the server hosting your web page. 3d. Finally, once uploaded, all future .PDF links that you want to be downloaded instead of opened in the browser will need to point to download.php?file=example.pdf, where example.pdf is the name of the PDF you wish for the user to download. Below is an example of what a full link could look like.
<a href="http://www.computerhope.com/download.php?file=example.pdf">Click here to download PDF</a>
|
Additional Information:
|