Image MIME/file type

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi All,

Is there a way to get PHP to change the mime type for a file sent to a  

I need my website viewers to be able to click a link to directly download  
and save an image, rather than the browser displaying it.

Is there some other way to make this happen?



Re: Image MIME/file type


Quoted text here. Click to load it

header('Content-Type: example/*')

Quoted text here. Click to load it


Quoted text here. Click to load it

  Look up Content-Disposition, but don't hold your breath.


Re: Image MIME/file type

Hi John

The images are purchased by the user, having them go through an instructions  
page for there browser telling them to right click and save as an all that  
i't would be nicer if they could click a download link, give it a name and  
job done.

Trouble is linking to the image results in the browser displaying the image,  
rather than downloading it.

Thanks for the reply.


Quoted text here. Click to load it

Re: Image MIME/file type

Quoted text here. Click to load it

You need to send content-type and content-disposition headers, example:

header('Content-type: image/png');
header('Content-disposition: attachment; filename="'$filename'"');

Remove caps to reply

Re: Image MIME/file type

Thanks again guys

I found this code on php website

$f = fopen("file.txt", "rb");
$content_len = (int) filesize($f, "file.txt");
$content_file = fread($f, $content_len);

$output_file = 'MXimage.jpg';

@ini_set('zlib.output_compression', 'Off');
header('Pragma: public');

header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
header('Content-Transfer-Encoding: none');
header('Content-Type: application/octetstream; name="' . $output_file .  
'"'); //This should work for IE & Opera
header('Content-Type: application/octet-stream; name="' . $output_file .  
'"'); //This should work for the rest
header('Content-Disposition: inline; filename="' . $output_file . '"');
header("Content-length: $content_len");

echo $content_file;

the filename MXimage.jpg is my own test image file.

when i point IE at it, i get a message about a 3 byte HTML file.  Why does  
that image file not come through? It seams that the script doesn't even pass  
the image data on.

is this something to do with the way PHP handeles the headers.  Its running  
on a Linux host.

Thanks again

Quoted text here. Click to load it

Site Timeline