Set the header( content-type: image/<ANY IMG FORMAT>)

user3663049 picture user3663049 · May 23, 2014 · Viewed 43.7k times · Source

The php file that handles the images I display only allows one image format, either .jpg, .png, .bmp, etc but not all. The imageName stores the file name of the image stored in the database including its format. this is my code, so far it doesn't work yet and I'm not sure if that's allowed. Can you help me fix it please?

$con = mysqli_connect("localhost","root","","tickets");
$ticket = 109;
$result = mysqli_query($con,"SELECT image, imageName FROM tix WHERE tktNum=$ticket");


while($row = mysqli_fetch_array($result))
{
    $image = $row['image'];
    $imageName = $row['imageName'];
    $format = substr( $imageName, -3 ); //gets the last 3 chars of the file name, ex: "photo1.png" gets the ".png" part
    header('content-type: image/' . $format);
}

Answer

John picture John · Feb 8, 2015

I just used this, just indicating it's an image but without specifying image type.

header("Content-type: image");

It seems to be working just fine regardless of file type. I tested with IE, Firefox, Safari and the Android browser.