link to .jpg, make save ato desktop ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

link to .jpg, make save ato desktop ?

Post 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.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

Using headers?

Code: Select all

<?
    header("Content-type: Application/octet-stream");
    header("Content-Disposition: attachment; filename=image.jpg"); 
    readfile("jpgif/image.jpg");
?>
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post by scheinarts »

how would I put that in '<a href="' . php code here . '">Download JPG</a>'
njcu
Forum Newbie
Posts: 8
Joined: Wed Aug 01, 2007 11:55 am
Location: Homestead, FL

Post 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);
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Safari downloads the script (you can see the code)?
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post 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.
njcu
Forum Newbie
Posts: 8
Joined: Wed Aug 01, 2007 11:55 am
Location: Homestead, FL

Post 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
Post Reply