View Full Version : rieving image types/mime types

08-24-2008, 02:41 AM
Hi, I'd like to retireve the file [mime] type (such as image/jpeg, etc) from an image that already exists on my file system. I know how to do this already in an image upload, but i've searched for a way to retrieve mime type of existing image -- no luck.

Any help is much appreciated.

08-24-2008, 01:17 PM

08-24-2008, 06:33 PM
If you know the filename this will pull up some info about the file.
$image = $_SERVER['DOCUMENT_ROOT'].'/images/Lain.JPG';
$imageinfo = GetimageSize($image);

[0] => 1024
[1] => 768
[2] => 2
[3] => width="1024" height="768"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
[0] = width
[1] = height
[2] = indicates the type of image (dunno what that means)
[3] = width and height together
[bits] = number of bits per color
[channels] = channels per pixel (don't know what this means either)
[mime] = jpg, gif, etc.
If you want to list the images in a given folder use this:

$imgdir = 'images'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
$a_img[] = $imgfile;
reset ($a_img);
$totimg = count($a_img); // total image number
for($x=0; $x < $totimg; $x++)
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
echo 'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />';

techietim, could you give an example of how fileinfo works? I could not get their examples to work.

08-24-2008, 07:03 PM
As long as you know you will only be working with images, you can use getimagesize() (http://php.net/getimagesize) like james438 posted.

If you need to find the mime type of anything else, you should use the Fileinfo (http://us3.php.net/fileinfo) extension, like techietem posted. Do not use mime_content_type().