# JPEG file download via PHP in IE



## positron (Mar 9, 2005)

I am trying to download a JPG file through a php page. The code is pretty straight forward, it obtains the filename from the query string, reads the file and outputs to the browser. Here is the code:

<?php
$filename = $_GET["file"];
$bytes = filesize("PATH_TO_FOLDER/$filename");
header("Content-type: application/jpg");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-length: $bytes");
//Read the file and output to browser
@readfile("PATH_TO_FOLDER/$filename");
//Code to record the download in database
?>

Originally I had "Content-type: image/jpeg"), but later on changed to "application/jpg" after reading through some forums on web. Both the content-types work fine in all browsers on a Windows PC, but they behave very differently with IE5.2 on MacOSX and IE5.1 on Mac OS9. The "Save As.." box does not open and the image gets displayed directly on the window. If I try to save the image by Ctrl+Click and Download to Disk, it shows the filename as "download.php" instead of the correct name "FILENAME.jpg" given in the Content-disposition header.

I did a lot of google and read in several forums that IE on Mac ignores the Content-disposition header. Is there any workaround for this. My aim is to display the "Save As.." dialog box on Mac IE (OSX and OS9), display the correct filename in the save as dialog box and allow the user to save the file through the dialog box. 

(In Mozilla (Mac) it will download the file (but you have to be very quick as it tends to close the "What should Mozilla do with this file?" dialog quickly if you don't select the 'save' option and click 'ok'). Safari seems to be the only browser happy to download a .jpg file onto the disk.)

Any suggestions or pointers would be appreciated.


----------



## andehlu (Mar 9, 2005)

could you tar or zip the file on the fly and prompt for that download? The browser may know what to do a bit better with this format maybe?

http://us4.php.net/manual/en/ref.zip.php


----------



## chornbe (Mar 9, 2005)

*nods* that was what I was going to suggest, too. Wrap it up in some non-media-type file for standard downloading.


----------



## positron (Mar 9, 2005)

Thanks for that, although I'd rather users didn't have to deal with unzipping - it may solve my main problem ... I'll give it a shot.


----------



## chornbe (Mar 9, 2005)

Hmm... I wonder if you could just application/octet-stream it?


----------



## andehlu (Mar 9, 2005)

how about out putting the image into a pop up window with instructions to right click  - save it?


----------



## aicul (Mar 10, 2005)

Lived this also.

Some browsers will automatically process files by their extensions as per previous downloads. IE does this. Download an unknown file (say abc.XYZ) and it will ask you how to process the .XYZ extension. Look carefully the dialog box actually says something like (do this for all files with this extension). From then on all files with extension .XYZ will be processed as indicated in the dialog box. Don't forget that this processing will overrule your PHP code as the setting is local to the client.

I have never-ever, found how to tell IE to change these local settings.

So you don't actually have to repackage, just rename the extension to some unknown extension.


----------



## phirana (Apr 19, 2005)

I had a similar problem - it works in every browser. The only one where it's a little dodgy is in IE in OSX, althought you are prompted to download it, the default file is download.php


----------

