Alex Rivera | Logout

Output an Image in PHP

Asked 2009-12-05T10:42:44.553
77

I have an image $file ( eg ../image.jpg )

which has a mime type $type

How can I output it to the browser?

Edit
Report

1 Answer

1

You can use header to send the right Content-type :

header('Content-Type: ' . $type);

And readfile to output the content of the image :

readfile($file);


And maybe (probably not necessary, but, just in case) you'll have to send the Content-Length header too :

header('Content-Length: ' . filesize($file));


Note : make sure you don't output anything else than your image data (no white space, for instance), or it will no longer be a valid image.

answered 2009-12-05T10:47:02.447

Your Answer