I-AM-OBODO Posted February 26, 2019 Share Posted February 26, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/ Share on other sites More sharing options...
I-AM-OBODO Posted February 26, 2019 Author Share Posted February 26, 2019 Oops! somehow through trial and error i got it working. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564834 Share on other sites More sharing options...
micky007 Posted February 26, 2019 Share Posted February 26, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564859 Share on other sites More sharing options...
I-AM-OBODO Posted February 26, 2019 Author Share Posted February 26, 2019 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! Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564861 Share on other sites More sharing options...
ginerjm Posted February 26, 2019 Share Posted February 26, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564866 Share on other sites More sharing options...
maxxd Posted February 27, 2019 Share Posted February 27, 2019 (edited) 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 February 27, 2019 by maxxd Forgot to type all the words... Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564869 Share on other sites More sharing options...
cyberRobot Posted February 27, 2019 Share Posted February 27, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564878 Share on other sites More sharing options...
cyberRobot Posted February 27, 2019 Share Posted February 27, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564881 Share on other sites More sharing options...
maxxd Posted February 28, 2019 Share Posted February 28, 2019 @cyberRobot - I had the same thought about the 'download' attribute, but looking it up on MDN it's a legit attribute. Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564911 Share on other sites More sharing options...
cyberRobot Posted February 28, 2019 Share Posted February 28, 2019 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! Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564914 Share on other sites More sharing options...
I-AM-OBODO Posted February 28, 2019 Author Share Posted February 28, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564935 Share on other sites More sharing options...
cyberRobot Posted February 28, 2019 Share Posted February 28, 2019 9 minutes ago, I-AM-OBODO said: In between, The download attribute is not supported by older browsers though Good to know, thanks ?☺️ Quote Link to comment https://forums.phpfreaks.com/topic/308396-echo-download-link/#findComment-1564937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.