Jump to content

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

Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/
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.

Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564859
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!

Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564861
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...
Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564869
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.

Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564878
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.

Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564881
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!

Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564914
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

Link to comment
https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564935
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.