Page 1 of 1
link to .jpg, make save ato desktop ?
Posted: Wed Aug 01, 2007 12:45 pm
by scheinarts
Hi,
I am wondering if there is a way so that when a user clicks on a link that ends in .jpg, instead of opening the image in browser, it would just bring the save to desktop request. If it is possible, would it be the same code for Windows and Mac?
I know this is already natural in the case of .zip files, but no clue how to make it work for jpgs.
Any help greatly appreicated.
Thanks.
Posted: Wed Aug 01, 2007 12:50 pm
by thiscatis
Using headers?
Code: Select all
<?
header("Content-type: Application/octet-stream");
header("Content-Disposition: attachment; filename=image.jpg");
readfile("jpgif/image.jpg");
?>
Posted: Wed Aug 01, 2007 1:00 pm
by scheinarts
how would I put that in '<a href="' . php code here . '">Download JPG</a>'
Posted: Wed Aug 01, 2007 1:12 pm
by njcu
HTML LINK PAGE
Code: Select all
<a href="downloadimage.php?path=filepath.jpg" target=_blank>image download</a>
PHP DOWNLOAD PAGE
Code: Select all
$path=$_GET['path'];
function ForceDownloadImage($path_to_image)
{
//get filename
$filename = basename($path_to_image);
//sent the right headers
header("Content-Transfer-Encoding: binary");
header("Content-Type: image/jpg");
header("Content-Disposition: attachment; filename=$filename");
//start feeding with the file
readfile($path_to_image);
}
ForceDownloadImage($path);
Posted: Wed Aug 01, 2007 5:45 pm
by scheinarts
thank you njcu, it works great. In Firefox, IE 6 and 7, works beatiful. But in Safari, it actually downloads the whole download.php file and NOT the image? How can this be fixed for it to work in Safari
Posted: Wed Aug 01, 2007 5:48 pm
by feyd
Safari downloads the script (you can see the code)?
Posted: Wed Aug 01, 2007 6:28 pm
by scheinarts
well, it download the php file, not the image, but when you open the php file in dreamweaver, its either corrupted or encrypted, since its a bunch on weird charcaters
Posted: Wed Aug 01, 2007 6:43 pm
by feyd
...and if you rename that file to say foo.jpg.. you'll find a picture most likely. Safari simply chose the "wrong" filename.
Posted: Thu Aug 02, 2007 2:43 pm
by scheinarts
Im sure what you mean by changing the file name, since its loading an image name dynamically every time
download.php?filename=image123.jpg
So I dont think I can change the filename to something.jpg, since its not one same image to be downloaded everytime.
Thanks.
Posted: Thu Aug 02, 2007 3:14 pm
by njcu
it's probably what feyd said
the image is downloading as a file with a php extension, if you change the extension of the "download.php" file to "download.jpg" you may be able to view the image... OR it may be downloading the jpg file and attaching some php source to the beginning of the file... *shrug*
I'll test it out on safari a bit later and see what i can come up with