Jump to content

save image button


geordie_b

Recommended Posts

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

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
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.