Dragosvr92 Posted July 1, 2010 Share Posted July 1, 2010 Hello i want to make a Link that when its clicked it forces the download by php of a .PNG Image from a specified directory like <a href="http://www.My_Image_Site.com/image.png">Download Link</a> dos anyone have a idea what will be the php script like ? i hope i posted it where it should .. edit : added code tags Link to comment https://forums.phpfreaks.com/topic/206412-force-download/ Share on other sites More sharing options...
simshaun Posted July 1, 2010 Share Posted July 1, 2010 Not being a smart-alec this time, but did you check Google? http://www.google.com/search?q=php+force+download Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1079805 Share on other sites More sharing options...
Dragosvr92 Posted July 1, 2010 Author Share Posted July 1, 2010 Thanks , i googled myself for Download image onclick and found this Site]http://stackoverflow.com/questions/1227157/html-javascript-download-a-picture-onclick]Site and come up with this script <?php if (isset($_GET['ID'])){ $file = 'http://img.msg.yahoo.com/avatar.php?format=jpg&yids=' .$_GET['ID']; header("Content-Type: application/octet-stream; "); header("Content-Transfer-Encoding: binary"); header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"'); readfile($file); } ?> But after i submit $_GET['ID'] it starts to download the image i only want to add a link so when its clicked it shows the download box Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1079815 Share on other sites More sharing options...
Dragosvr92 Posted July 1, 2010 Author Share Posted July 1, 2010 Dosnt anyone have any idea how may i execute that script onclick ? :| Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1079832 Share on other sites More sharing options...
Dragosvr92 Posted July 2, 2010 Author Share Posted July 2, 2010 i thought this forums were fast for support Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080020 Share on other sites More sharing options...
litebearer Posted July 2, 2010 Share Posted July 2, 2010 its a weekday. people work. eat play golf, fish, swim AND then come to see it there is a poor soul in need of FREE help. Be patient Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080022 Share on other sites More sharing options...
Dragosvr92 Posted July 2, 2010 Author Share Posted July 2, 2010 its a weekday. people work. eat play golf, fish, swim AND then come to see it there is a poor soul in need of FREE help. Be patient Thanks for your reply lite ... you re right .. Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080023 Share on other sites More sharing options...
Garethp Posted July 2, 2010 Share Posted July 2, 2010 No, there's no way to force download the way you want. You can start a download, like you wanted, but there's no way to make a download go without going through the browser, and there's no way to have the browser show the progress. If there were a way, it would be a major security hole. Imagine if you could, people could download files onto your computer without knowing about it at all Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080024 Share on other sites More sharing options...
Dragosvr92 Posted July 2, 2010 Author Share Posted July 2, 2010 mate ... that download gows within a browser if you run that script on a php page it will just start to open the download box you will click ok and it will download it i only want that php code to be executed only when someone clicks a link Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080030 Share on other sites More sharing options...
Garethp Posted July 2, 2010 Share Posted July 2, 2010 Oh, well that's easy <?php if (isset($_GET['ID'])){ $file = 'images/png' . $_GET['ID'] . '.png'; header("Content-Type: application/octet-stream; "); header("Content-Transfer-Encoding: binary"); header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"'); readfile($file); } ?> For that script, just have that as image.php and when you want them to download an image, just link to <a href="image.php?ID=3">Click Here to Download Image</a> and it'll download images/png3.png Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080034 Share on other sites More sharing options...
Dragosvr92 Posted July 2, 2010 Author Share Posted July 2, 2010 i dont understand your script but if i add it to my page it starts the download while submitting the form Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080036 Share on other sites More sharing options...
Dragosvr92 Posted July 2, 2010 Author Share Posted July 2, 2010 i put this page up http://rising-dead.com/Look/ so u may see better what i want to do.. its script is : <?php if (isset($_GET['ID'])){ $file = 'http://img.msg.yahoo.com/avatar.php?format=png&yids=' .$_GET['ID']; header("Content-Type: application/octet-stream; "); header("Content-Transfer-Encoding: binary"); header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"'); readfile($file); } ?> <form action="<? $_SERVER['PHP_SELF']; ?>" method="get">ID: <input type="text" name="ID"/> <input type="submit" value="Download" title="Submits" /> <input type="reset" value="Cancel" title="resets the form" /><br /> </form> When you enter a yahoo id for example rdragos18 it will show the download box to download my avatar i only want it to show the download box when you click a link is there any thing like : <?php if (THE XX LINK WAS CLICKED){ echo 'Executed script' } ?> only execute the script when the link is clicked Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080049 Share on other sites More sharing options...
Cagecrawler Posted July 2, 2010 Share Posted July 2, 2010 So you want the page to only work when you click a link rather than if you typed it into the brower url bar for example? If the link and the script are on the same domain, you can use $_SESSION to sign the link. Page the link is on: <?php session_start(); $hash = sha1(time()); $id = "rdragos18"; $_SESSION['hash'] = $hash; echo "<a href='image.php?hash=$hash&id=$id'>Download avatar</a>"; Download script: <?php session_start(); if($_GET['hash'] == $_SESSION['hash']) { //Do download stuff. } else { //Do error stuff. } I'm not suggesting that my code is secure but it gives you a start point to work from. Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080062 Share on other sites More sharing options...
Dragosvr92 Posted July 2, 2010 Author Share Posted July 2, 2010 that dosnt really help me much if you look into my script well you will see that the image/avatar is not hosted on my site Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080432 Share on other sites More sharing options...
mattal999 Posted July 2, 2010 Share Posted July 2, 2010 that dosnt really help me much if you look into my script well you will see that the image/avatar is not hosted on my site Your form works as it should... You press Download and it starts downloading. I assume that you wish to preview the image before you download it, so you would need to use a form handler to check if the form has been submitted if(isset($_POST['id'])) { and then have a link to the actual download php script from there. Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080438 Share on other sites More sharing options...
Cagecrawler Posted July 3, 2010 Share Posted July 3, 2010 that dosnt really help me much if you look into my script well you will see that the image/avatar is not hosted on my site It doesn't matter where the images are hosted, you only need to have the download script and the link on your site. My script only adds to what you've already got which seems to work fine. Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080653 Share on other sites More sharing options...
Dragosvr92 Posted July 3, 2010 Author Share Posted July 3, 2010 Nevermind I've Done it myself Like this: //Download Avatar Script if (isset($_GET['Download'])){ $file = 'http://img.msg.yahoo.com/avatar.php?format=png&yids=' .$_GET['ID']; header("Content-Type: application/octet-stream; "); header("Content-Transfer-Encoding: binary"); header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"'); readfile($file); } //End <?echo '<a href="http://localhost/MyYahooScript/test.php?ID='.$_GET['ID'].'&Download=+">Download Avatar</a>';?> it was pretty simple.. Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1080751 Share on other sites More sharing options...
Dragosvr92 Posted July 4, 2010 Author Share Posted July 4, 2010 Hmm i ve found a small problem with my script ... it seems that it also downloads the page's source into the image at the end of the image script there is a comma is there some way to remove everything after the comma with some preg replace or something ? Here is my image script http://rising-dead.com/test.txt Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1081005 Share on other sites More sharing options...
kenrbnsn Posted July 4, 2010 Share Posted July 4, 2010 You need to put an exit after the readfile() so your script exits after sending the picture. <?php readfile($file); exit(); ?> Ken Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1081018 Share on other sites More sharing options...
Dragosvr92 Posted July 4, 2010 Author Share Posted July 4, 2010 Thank You Ken ! I've added exit; after readfile($file); i tryed to add an exit tag but i didnt knew exactly where to put it at Thank You Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1081046 Share on other sites More sharing options...
Dragosvr92 Posted July 4, 2010 Author Share Posted July 4, 2010 lol i find a other bug... the image script starts after a empty line that stops displaying the image like : 1. 2. ‰PNG the ‰PNG line must be the first at the top not on the second line Nevermind i figured what was it !! it was because i had a space after the ?> ending tag <?php Some Script ?> //Space was here <?php image script ?> Link to comment https://forums.phpfreaks.com/topic/206412-force-download/#findComment-1081068 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.