Php A Href Download Link

HTML Download Link How to HTML Download Link? The only difference is that you have to set the HREF property equal to your download file, rather than specifying a web URL. HTML Source Code: Download PDF. User can Right click or option-click the link and choose 'Save As.'

Active11 months ago

I have some big size PDF catalogs at my website, and I need to link these as download. When I googled, I found such a thing noted below. It should open the 'Save As...' popup at link click...

But it doesn't work :/ When I link to a file as below, it just links to file and is trying to open the file.

UPDATE (according to answers below):

As I see there is no 100% reliable cross-browser solution for this. Probably the best way is using one of the web services listed below, and giving a download link...

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
designer-trying-codingdesigner-trying-coding
2,79616 gold badges64 silver badges95 bronze badges

17 Answers

From an answer to Force a browser to save file as after clicking link:

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Ayush GuptaAyush Gupta
4,2431 gold badge17 silver badges17 bronze badges
Php a href linkGabLeRoux
8,6446 gold badges42 silver badges65 bronze badges
DrWakyDrWaky

Meta tags are not a reliable way to achieve this result. Generally you shouldn't even do this - it should be left up to the user/user agent to decide what do to with the content you provide. The user can always force their browser to download the file if they wish to.

If you still want to force the browser to download the file, modify the HTTP headers directly. Here's a PHP code example:

Note that this is just an extension to the HTTP protocol; some browsers might ignore it anyway.

peterjwest
2,7362 gold badges24 silver badges40 bronze badges
Karel PetranekKarel Petranek
13.5k4 gold badges36 silver badges63 bronze badges

I had this same issue and found a solution that has worked great so far. You put the following code in your .htaccess file:

It came from Force a File to Download Instead of Showing Up in the Browser.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
TonyTony

I found a very simple solution for Firefox (only works with a relative rather than a direct href): add type='application/octet-stream':

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
UserUser

Generally it happens, because some browsers settings or plug-ins directly open PDF in the same window like a simple web page.

The following might help you. I have done it in PHP a few years back. But currently I'm not working on that platform.

Save the above as download.php.

Save this little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly. If you want to serve files other than PDF, remove or edit line 5.

You can use it like so:

Add the following link to your HTML file.

Reference from: This blog

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
AshayAshay

Try adding this line to your .htaccess file.

I hope it'll work as it is browser independent.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Sharad GautamSharad Gautam

Just put the below code in your .htaccess file:

Or you can also do trick by JavaScript

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Download
manish1706manish1706

I just used this, but I don't know if it works across all browsers.

It works in Firefox:

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
NowLiveLoveNowLiveLove

A really simple way to achieve this, without using external download sites or modifying headers etc. is to simply create a ZIP file with the PDF inside and link directly to the ZIP file. This will ALWAYS trigger the Save/Open dialog, and it's still easy for people to double-click the PDF windows the program associated with .zip is launched.

BTW great question, I was looking for an answer as well, since most browser-embedded PDF plugins take sooo long to display anything (and will often hang the browser whilst the PDF is loading).

Mr. BungleMr. Bungle

A very easy way to do this, if you need to force download for a single link on your page, is to use the HTML5 download-attribute in the href-link.

See: http://davidwalsh.name/download-attribute

with this you can rename the file that the user will download and at the same time it forces the download.

There has been a debate whether this is good practise or not, but in my case I have an embedded viewer for a pdf file and the viewer does not offer a download link, so i have to provide one separately. Here I want to make sure the user does not get the pdf opened in the web browser, which would be confusing.

This won't necessary open the save as-dialog, but will download the link straight to the preset download destination. And of course if your doing a site for someone else, and need them to write in manually attributes to their links is probably a bad idea, but if there is way to get the attribute into the links, this can be a light solution.

JJxyzJJxyz

A server-side solution is more compatible, until the 'download' attribute is implemented in all the browsers.

One Python example could be a custom HTTP request handler for a filestore. The links that point to the filestore are generated like this:

Here is the code:

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
yuceryucer
1,6021 gold badge20 silver badges33 bronze badges

After the file name in the HTML code I add ?forcedownload=1

This has been the simplest way for me to trigger a dialog box to save or download.

ByteHamster
4,2879 gold badges29 silver badges49 bronze badges
BonnieBonnie

If you have a plugin within the browser which knows how to open a PDF file it will open directly. Like in case of images and HTML content.

So the alternative approach is not to send your MIME type in the response. In this way the browser will never know which plugin should open it. Hence it will give you a Save/Open dialog box.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
sushil bharwanisushil bharwani
17.6k29 gold badges82 silver badges119 bronze badges

I just had a very similar issue with the added problem that I needed to create download links to files inside a ZIP file.

I first tried to create a temporary file, then provided a link to the temporary file, but I found that some browsers would just display the contents (a CSV Excel file) rather than offering to download. Eventually I found the solution by using a servlet. It works both on Tomcat and GlassFish, and I tried it on Internet Explorer 10 and Chrome.

The servlet takes as input a full path name to the ZIP file, and the name of the file inside the zip that should be downloaded.

Inside my JSP file I have a table displaying all the files inside the zip, with links that say: onclick='download?zip=<%=zip%>&csv=<%=csv%>'

The servlet code is in download.java:

To compile on Tomcat you need the classpath to include tomcatlibservlet-api.jar or on GlassFish: glassfishlibj2ee.jar

But either one will work on both. You also need to set your servlet in web.xml.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
mljmmljm

This is old post but here is the one my solution in JavaScript what using jQuery library.

You just need to use class force-download inside your <a> tag and will force download automaticaly. You also can add it to parent div and will pickup all links inside it.

Example:

This is great for WordPress and any other systems or custom websites.

Ivijan Stefan StipićIvijan Stefan Stipić
2,4453 gold badges28 silver badges57 bronze badges

With large PDF files the browser hangs.In Mozilla, menu ToolsOptionsApplications, then next to the content type Adobe Acrobat document.In the Action drop down, select Always ask.

This did not work for me, so what worked was:

Menu Tools* → Add-onsAdobe Acrobat (Adobe PDF plugin for Firefox) → DISABLE.Now I am able to download e-books!

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Link
JoojooJoojoo

protected by CommunityApr 6 '17 at 9:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged htmlpdfdownloadmeta-tagscontent-disposition or ask your own question.

Active12 months ago

I'm trying to use a php variable to add a href value for a link in an echo statement.

Here's a simplified version of the code I want to use. I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.

Any pointers appreciated.

jasonbradberryjasonbradberry
4852 gold badges14 silver badges28 bronze badges

5 Answers

Try like

HTML in PHP :

Or even you can try like

Or you can use PHP in HTML like

PHP in HTML :

Gautam3164Gautam3164
25.3k6 gold badges52 silver badges73 bronze badges

you can either use

or

if you use double quotes you can insert the variable into the string and it will be parsed.

Davide De SantisDavide De Santis

Basically like this,

<?php$link = '; // Link goes here!print '<a href='.$link.'>Link</a>';?>

Ryan PaluchRyan Paluch

as simple as that: echo '<a href='.$link_address.'>Link</a>';

JayJay
2,7775 gold badges19 silver badges32 bronze badges

If you want to print in the tabular form with, then you can use this:

Php A Href Example

Alaa MoneamAlaa Moneam

protected by CommunityOct 9 '15 at 17:40

Php A Href

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged phpvariablesechohrefdouble-quotes or ask your own question.