Jump to content
I-AM-OBODO

Echo Download Link

Recommended Posts

Hi guys,
I stored the path of a file in a database and the file in a folder, now i want users to click on a link and be able to download the file.
I tried:

	  echo " Download <a href=".$file_path." download>Here</a>"; ?>
	

Didnt work, just took me to the path of the image. What i want is a link and when clicked the download window pops out.

 

Thanks

Share this post


Link to post
Share on other sites

Just so you know the correct way is

echo 'Download <a href="LINK HERE">Here</a>';

Remember if you start your PHP coding with a double quote (") then your going to have to use a single quote for defining the URL, or the other way around like the example above.

Share this post


Link to post
Share on other sites
36 minutes ago, micky007 said:

Just so you know the correct way is


echo 'Download <a href="LINK HERE">Here</a>';

Remember if you start your PHP coding with a double quote (") then your going to have to use a single quote for defining the URL, or the other way around like the example above.

The concatenation does the job. The work of concatenation(the dot) is to join. So mine as well is very ok. So whichever way you feel like, its up to you!

Share this post


Link to post
Share on other sites

I"m sure micky didn't mean to imply any wrongdoing on your part.  He just pointed out that his style/method gives a cleaner, easier-to-read line of code.  Yes - yours worked but so does his with less punctuation and quoting.

Share this post


Link to post
Share on other sites

Actually, what you posted and what @micky007 posted aren't quite equivalent. Here's yours:

echo " Download <a href=".$file_path." download>Here</a>";

When parsed, this turns into:

<a href=path/to/file download>Here</a>

True, technically attributes don't need to be enclosed in quotes (last time I checked - that may be wrong now), but it is generally considered a good idea.

Edited by maxxd
Forgot to type all the words...

Share this post


Link to post
Share on other sites
11 hours ago, maxxd said:

True, technically attributes don't need to be enclosed in quotes (last time I checked - that may be wrong now), but it is generally considered a good idea.

Using quotes is especially important if you ever get a file that contains spaces. Whether you use single or double quotes for your attribute values shouldn't matter. I typically use double quotes because I had problems in the past with specific attributes not working with single quotes when the value contained spaces. I have no idea if that's still the case. I also don't remember which tag/attribute I was having problems with.

Share this post


Link to post
Share on other sites
14 hours ago, ginerjm said:

He just pointed out that his style/method gives a cleaner, easier-to-read line of code.  Yes - yours worked but so does his with less punctuation and quoting.

Once you adjust micky007's solution to work with PHP variables, it's not going to be much different than what I-AM-OBODO posted.

echo 'Download <a href="' . $file_path . '">Here</a>';

In case it comes up, I agree that attribute values should be enclosed in quotes. It's also unclear as to why there's an extra "download" in the open anchor tag. Personally, I would likely change the code to the following:

<a href="<?=$file_path?>">Download Here</a>

Of course, my decision to change the code would be dependent on whatever code surrounds the above link.

Note that I would also change the "Download Here" text to something that made more sense when read out of context (e.g. Download Brochure). That way people using assistive technologies, like screen readers, would better understand what the link is for when they are using the technology to jump from link to link.

Share this post


Link to post
Share on other sites

@cyberRobot - I had the same thought about the 'download' attribute, but looking it up on MDN it's a legit attribute.

Share this post


Link to post
Share on other sites
10 hours ago, maxxd said:

@cyberRobot - I had the same thought about the 'download' attribute, but looking it up on MDN it's a legit attribute.

Interesting. That would explain why the OP didn't need to write a script to force the browser to download the file. I'll have to try out the attribute sometime. Thanks!

Share this post


Link to post
Share on other sites
5 hours ago, cyberRobot said:

Interesting. That would explain why the OP didn't need to write a script to force the browser to download the file. I'll have to try out the attribute sometime. Thanks!

In between, The download attribute is not supported by older browsers though

Share this post


Link to post
Share on other sites
9 minutes ago, I-AM-OBODO said:

In between, The download attribute is not supported by older browsers though

Good to know, thanks 😔☺️

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.