I want to get the file type (eg. image/gif) by URL using PHP.I had tried
You are not going wrong anywhere. exif_imagetype returns the value of one of the image type constants: http://php.net/manual/en/image.constants.php
If you would like to convert this to an extension string, you could use a switch statement:
$typeString = null;
$typeInt = exif_imagetype($image_path);
switch($typeInt) {
case IMG_GIF:
$typeString = 'image/gif';
break;
case IMG_JPG:
$typeString = 'image/jpg';
break;
case IMG_JPEG:
$typeString = 'image/jpeg';
break;
case IMG_PNG:
$typeString = 'image/png';
break;
case IMG_WBMP:
$typeString = 'image/wbmp';
break;
case IMG_XPM:
$typeString = 'image/xpm';
break;
default:
$typeString = 'unknown';
}
You may want to change the order to most to least frequently expected for better performance.