geordie_b Posted August 2, 2007 Share Posted August 2, 2007 Hi I have a image gallery and I would like to add a button next to each image to let the user save the image to their computer rather than prompting them to right click. Has anyone got some code which would do this? I have been googling but cant seem to find the right search phrase. thanks Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/ Share on other sites More sharing options...
Salis Posted August 2, 2007 Share Posted August 2, 2007 I'd say use a header to force a download. I'm not sure how your gallery is set up. I'm working on my my self. I would make a new file named download.php and stick the code below in it. Of course you'd have to modify it to fit your needs. Then if some one wants to download the image, they click the link and the download.php file "GET" the image name and forces a download. <?php $ImageName = stripslashes($_GET['img']); // I'd actually pull this from my database $ImageLoc = "./gallery/images/$ImageName"; // This too header("Cache-Control: public, must-revalidate"); header("Content-Type: application/octet-stream"); header("Content-Length: " . filesize($ImageLog) ); header("Content-Disposition: attachment; filename=" .$ImageName); header("Content-Transfer-Encoding: binary\n"); $fp = fopen($ImageLoc, 'rb'); $buffer = fread($fp, filesize($ImageLoc)); fclose ($fp); print $buffer; ?> Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314103 Share on other sites More sharing options...
Caesar Posted August 2, 2007 Share Posted August 2, 2007 I do something similar to this with other file types but I don't think I've tried it with images....possibly something like... <?php fopen('temp/'.$filename,"w"); fclose($filehandle); header("Content-type: force-download"); header("Content-type: jpeg"); header("Content-transfer-encoding: binary\n"); header("Content-disposition: attachment; filename=\"$filename\""); unlink('temp/'.$filename); ?> You'll have to fill in the blanks and edit it....but maybe something like that can be done using images as well. I don't see why not. (Note: Again, have only tried this with documents and other file types) Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314104 Share on other sites More sharing options...
Caesar Posted August 2, 2007 Share Posted August 2, 2007 Ah Salis beat me to it. But yeah...using headers. Though if Salis has done this...I would try his example. :-) Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314106 Share on other sites More sharing options...
geordie_b Posted August 2, 2007 Author Share Posted August 2, 2007 I'd say use a header to force a download. I'm not sure how your gallery is set up. I'm working on my my self. I would make a new file named download.php and stick the code below in it. Of course you'd have to modify it to fit your needs. Then if some one wants to download the image, they click the link and the download.php file "GET" the image name and forces a download. <?php $ImageName = stripslashes($_GET['img']); // I'd actually pull this from my database $ImageLoc = "./gallery/images/$ImageName"; // This too header("Cache-Control: public, must-revalidate"); header("Content-Type: application/octet-stream"); header("Content-Length: " . filesize($ImageLog) ); header("Content-Disposition: attachment; filename=" .$ImageName); header("Content-Transfer-Encoding: binary\n"); $fp = fopen($ImageLoc, 'rb'); $buffer = fread($fp, filesize($ImageLoc)); fclose ($fp); print $buffer; ?> hi thanks for the code, what is the variable $Imagelog? Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314111 Share on other sites More sharing options...
Salis Posted August 2, 2007 Share Posted August 2, 2007 Ah Salis beat me to it. But yeah...using headers. Though if Salis has done this...I would try his example. :-) You can give it a shot. I haven't tested this script yet. But the nice thing is this should work for ANY file you want to force download Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314113 Share on other sites More sharing options...
Salis Posted August 2, 2007 Share Posted August 2, 2007 hi thanks for the code, what is the variable $Imagelog? Oops.. sorry, my mistake. $Imagelog should be $ImageLoc Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314115 Share on other sites More sharing options...
geordie_b Posted August 2, 2007 Author Share Posted August 2, 2007 not sure what im doing wrong but both of the suggested scripts will save a file but then when i try and open the file it cant be read. Any ideas what I could have done wrong? Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314134 Share on other sites More sharing options...
Salis Posted August 2, 2007 Share Posted August 2, 2007 Could you post a demo for me to see? I'll rewite the script for ya. Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314137 Share on other sites More sharing options...
geordie_b Posted August 2, 2007 Author Share Posted August 2, 2007 all i have done to test is, change the first 2 lines of code to $ImageName = 'myfile.jpg'; $ImageLoc = ' '; as the jpeg is in the same directory as the file Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314141 Share on other sites More sharing options...
Salis Posted August 2, 2007 Share Posted August 2, 2007 try this: $ImageName = "myfile.jpg"; $ImageLoc = " ./$ImageName"; When we open a file to read the data, we need to know how much to read so we use filesize(). filesize need to know where the file is. Since it's in the same directory I've add the ./$ImageName to say "look in this folder for $ImageName" I hope this helps. If no could you post you full download code, including modifications? Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314160 Share on other sites More sharing options...
geordie_b Posted August 2, 2007 Author Share Posted August 2, 2007 I got it working by doing this $ImageName = 'jen.jpg'; $ImageLoc = 'images/'.$ImageName; it didnt like these lines when i tried to use them $ImageLoc = ''.$ImageName; $ImageLoc = '/'.$ImageName; im not actually going to have the image in the same directory but its weird that it wont work if it is. Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314171 Share on other sites More sharing options...
Salis Posted August 2, 2007 Share Posted August 2, 2007 I got it working by doing this $ImageName = 'jen.jpg'; $ImageLoc = 'images/'.$ImageName; it didnt like these lines when i tried to use them $ImageLoc = ''.$ImageName; $ImageLoc = '/'.$ImageName; im not actually going to have the image in the same directory but its weird that it wont work if it is. You have to remember there is a difference between a quote " and a tick ' Also if you use a single / you're actually saying "look in my main home directory". In other words if you have /jen.jpg the script will look for the file in www.example.com/jen.jpg when you may be trying to look in www.example.com/gallery/images/jen.jpg But I'm glad to hear you got it to work. Happy to help. Quote Link to comment https://forums.phpfreaks.com/topic/63064-save-image-button/#findComment-314176 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.