13

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

kirti kadam
  • 131
  • 1
  • 1
  • 3
  • possible duplicate of [PHP how can i check if a file is mp3 or image file](http://stackoverflow.com/questions/2006632/php-how-can-i-check-if-a-file-is-mp3-or-image-file) - despite the question title a good deal of the answers given there are filetype independent – Gordon Jun 19 '10 at 09:47

7 Answers7

27
$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
  • 869
  • 10
  • 22
6

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;
 }
Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • From the getimagesize docs: "Do not use getimagesize() to check that a given file is a valid image. Use a purpose-built solution such as the Fileinfo extension instead." https://secure.php.net/manual/en/function.getimagesize.php – Stephen R Feb 14 '19 at 17:48
  • 1
    @StephenR The code above _does_ use the FileInfo extension when it's available as the first choice. Back in 2010 when I wrote this answer, `finfo_open` wasn't necessarily available though. It's part of PHP since 5.3.0. Before that, you had to install it via PECL. And not everyone could do that, e.g. shared hosting. So people needed a fallback. And while the other three are indeed not as reliable, they did get the job done. So I don't the see the point in pointing out that doc passage. – Gordon Feb 19 '19 at 11:56
4

I use the following code which IMO is more universal than in the first and the most upvoted answer:

$mimeType = mime_content_type($filename);
$fileType = explode('/', $mimeType)[0];

I hope it was helpful for anyone.

Paul Basenko
  • 895
  • 1
  • 9
  • 22
3

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
  • 50,859
  • 17
  • 87
  • 112
3
if(isset($_FILES['my_file'])) {
$mime = $_FILES['my_file']['type'];
if(strstr($mime, "video/")){
$filetype = "video";
}else if(strstr($mime, "image/")){
$filetype = "image";
}else if(strstr($mime, "audio/")){
$filetype = "audio";
}  
Randel Fordberg
  • 105
  • 1
  • 2
  • 11
  • 2
    While this code may answer the question, it would be better to explain how it solves the problem and why to use it. Code-only answers are not useful in the long run. – Tobias Liefke Jan 08 '16 at 07:39
  • This answer works for uploaded files only. Also, [the `'type'` is provided by the browser (if any)](http://php.net/manual/en/features.file-upload.post-method.php) and therefore may not be fully trusted. – Pang Jan 09 '16 at 05:52
0

Rather old question, but for others looking at this in the future, I would handle this like so:

function getType($file): string
{
    $mime_type = mime_content_type($file);

    return strtok($mime_type, '/');
}

This method utilises strtok to return the portion of the $mime_type string before the first /.

For example, let's say $file has a $mime_type of video/mp4, the getType method will return video.

Ben Carey
  • 16,540
  • 19
  • 87
  • 169
0

I use this code and it works very well.

$mimeType = $request->images->getMimeType();
$fileType = explode('/', $mimeType)[0];

if it was an image, this code will give you the image word in the $fileType and if it was a video this code will give you the video word in the $fileType, then you can check on it by the if conditions.

good luck

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36