How to create a link that launches a file download?

When the user clicks on the “Download PDF” link, I would like to see a download prompt so that the user can download the file.

Currently, the user is simply being transferred to the address of the PDF file.

For example:

<a..[what goes here??]..>Download PDF</a> 

This seems to require a combination of JavaScript and PHP.

Can someone give an example?

+6
javascript file php download
source share
7 answers

If you are in Apache, you can delete it in your .htaccess:

 <Files *.pdf> Header set Content-Disposition attachment </Files> 

This will send all PDF documents as downloads. mod_headers must be enabled.

+6
source share

Redirect to a PHP page using this code:

 <?php header('Content-disposition: attachment; filename=movie.mpg'); header('Content-type: video/mpeg'); readfile('movie.mpg'); ?> 

your code <a href should point to a specific page, and the read file will call your resource

Further reading

As a note, I agree that you should not override browser settings, but sometimes when the boss asks you just have to do it.

+7
source share
 You can also use the following code: <?php $file = filter_input(INPUT_GET, 'file'); $filename = basename($file); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=\"{$filename}\";"); readfile("your file path {$file}"); ?> 

This will cause any file extensions such as (.pdf, .docx, .zip, .csv, .txt, .html, .php, etc.) without opening directly in a new browser.

+2
source share

You can force the browser to save related content by adding an HTTP header to the web server response:

 Content-Disposition: attachment; filename=<default filename to save as> 

On the other hand, I really do not see any reason in overriding the user's browser configuration, which usually means that PDF documents should be opened in the browser by default, opened in a separate PDF viewer or saved to disk.

+1
source share

You can use the HTML5 download attribute in your tag.

 <a href="http://love4cats.com/kitten-catalogue.pdf" download>Download the CATalogue</a> 

You should check your analytics and make sure that your target browser supports this attribute. See caniuse.com entry for browser support.

+1
source share

It’s not clear what you mean by

Currently, the user is simply being transferred to the address of the PDF file.

But I suggest a simple approach:

 <a href='http://example.com/download.pdf'>Download PDF</a> 

The choice of downloading or opening a file is more of a browser setting.

0
source share

I don't believe there is a magical javascript / PHP solution for your problem, vanilla HTML:

 <a href="docco.pdf" title="this is a link to a pdf">download the PDF</a> 

will do what is required, it will direct the browser to request the resource "docco.pdf", it depends on the browser (and any plug-ins) to do something about it.

For example, there is a Firefox Download PDF addon that offers the user a choice of what to do with the popup.

0
source share

All Articles