Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to check a file is video type or not in php?
In PHP, you can check if a file is a video type by examining its file extension. The most common approach is to extract the extension using explode() and end() functions, then compare it against known video formats.
Basic Extension Check
Here's how to check for a single video format like MP4 −
<?php
$movieFileType = "demo.mp4";
if(strtolower(end(explode(".", $movieFileType))) == "mp4") {
echo "The movie ", $movieFileType, " is of video type.";
} else {
echo "The movie ", $movieFileType, " is not of video type.";
}
?>
The movie demo.mp4 is of video type.
Multiple Video Format Check
For a more comprehensive solution that checks multiple video formats −
<?php
function isVideoFile($filename) {
$videoExtensions = ['mp4', 'avi', 'mov', 'wmv', 'flv', 'webm', 'mkv'];
$extension = strtolower(end(explode('.', $filename)));
return in_array($extension, $videoExtensions);
}
// Test with different files
$files = ['movie.mp4', 'video.avi', 'document.pdf', 'clip.mov'];
foreach($files as $file) {
if(isVideoFile($file)) {
echo $file . " is a video file<br>";
} else {
echo $file . " is not a video file<br>";
}
}
?>
movie.mp4 is a video file video.avi is a video file document.pdf is not a video file clip.mov is a video file
Using pathinfo() Method
A more reliable approach using PHP's built-in pathinfo() function −
<?php
function checkVideoType($filename) {
$videoTypes = ['mp4', 'avi', 'mov', 'wmv', 'flv', 'webm'];
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
return in_array($extension, $videoTypes);
}
$testFile = "sample.mp4";
echo checkVideoType($testFile) ? "Video file detected" : "Not a video file";
?>
Video file detected
Conclusion
Use pathinfo() for better reliability when extracting file extensions. For multiple format support, store video extensions in an array and use in_array() to check against them efficiently.
Advertisements
