Jump to content

Echo Download Link


I-AM-OBODO

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
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
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
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.

Link to comment
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
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
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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.