PHP Find Videos in Directory

I use this PHP function to find videos in a given directory. It will find files recursively in the given directory (at the given path) and put them into the array provided.

/**
 * Recurse into directories at the given path, find videos, and put
 * their path into the vids array.
 *
 * @param {string} $path The path to the search directory.
 * @param {array} A referenced array, where paths to videos are stored.
 */
function find_vids($path, &$vids) {
    foreach (scandir($path) as $contents) {
        $file = rtrim($path, '/').'/'.$contents;

        if (!preg_match('|^\.|', $contents)) {
            if (is_dir($file)) {
                find_vids($file, $vids);
            }   
            else {
                $fileType = preg_split('|/|',mime_content_type($file))[0];

                if (strtolower($fileType) === 'video') {
                    array_push($vids, $file);
                }   
            }   
        }   
    }   
}

Leave a Reply

Your email address will not be published. Required fields are marked *