tags:

views:

39

answers:

3

how to check whether file is image or video type in php version 5.2.9

+2  A: 

You can check the MIME type using the finfo_file function

Example from the help page

<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
    echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
?>

EDIT: after better checking your question, this won't work, finfo functions require PHP 5.3.0

nico
+1  A: 
$mime = mime_content_type($file);
if(strstr($mime, "video/")){
    // this code for video
}else if(strstr($mime, "image/")){
    // this code for image
}

Should work for most file extentions.

Semas
+1  A: 

See my answer to

Example Code

 function getMimeType($filename)
 {
     $mimetype = false;
     if(function_exists('finfo_fopen')) {
         // open with FileInfo
     } elseif(function_exists('getimagesize')) {
         // open with GD
     } elseif(function_exists('exif_imagetype')) {
        // open with EXIF
     } elseif(function_exists('mime_content_type')) {
        $mimetype = mime_content_type($filename);
     }
     return $mimetype;
 }
Gordon
+1 this is the best and most platform-independent solution.
Pekka