Saturday 18 May 2013

Make link prompt visitor to download .PDF, .DOC, or other file


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.
  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.php3b. 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>

No comments:

Post a Comment

Do you think it could be useful for you? Share your thoughts with us!