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

Updated: 04/26/2017 by Computer Hope

Note: This document is for Webmasters who want .PDF, .DOC, or other file links to open in a specific way on their web page, not for changing file settings in your Internet browser.

PDFIn some situations, developers want to create a web page with links to an Adobe Acrobat .PDF, Microsoft Word .DOC, Microsoft Excel .XLS, or external program files. In these cases, they may want the browser to prompt the user to download the file instead of opening it in the browser window or an external program. There are a few different methods you use to achieve this effect.

Save / Save As option

Create a link to download the file on the web page using the <A HREF> HTML tag. Then, recommend to the web page viewer that they right-click the link and choose the option to Save or Save as the file. Viewers can then download and save the file to their computer.

Zip the file

Compress the file and create a .ZIP file or other compressed file format. Then, create a link to download the file on the web page using the <A HREF> HTML tag. By compressing the file into a ZIP file and creating a link to it, a web browser is unable to directly open the ZIP file. Instead, it will prompt the user to download the ZIP file or automatically download the ZIP file.

For example, the below HTML link would allow a web page viewer to download a file named, containing the file you compressed to create the ZIP file.

<a href="">

PHP scripting

Create the below PHP file that can be used to open .PDF files. It can also be modified to allow for the downloading of .DOC or other files.

  1. Create a new file named download.php
  2. After creating the file, copy and paste the below code into the PHP 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\"");
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";

  1. Save the file and upload to the server hosting the web page.
  2. Once uploaded, links to download a PDF file need to point to download.php?file=example.pdf, where example.pdf is the name of the PDF file you want users to download.

Below is an example of a full link using the PHP scripting.

<a href="">Click here to download PDF</a>

Additional information

  • See the PDF definition for further information about PDF files, questions and answers, and related links.