如果目录包含不同的文件类型,请检查 php


Check in php if directory contains different file types

检查

给定目录是否包含 *.png *.mp4 等的最佳方法是什么?我只想要一个真正的返回,如果所有这些文件类型都在目录中。

这是我当前的代码,它不起作用。 如果我只使用一种文件类型,它可以工作,但不返回 false:

var_dump(glob($video_dir.'*.txt,.jpg', GLOB_BRACE));

谢谢

// function to find files with specified extensions in a folder
function has_the_files( $dir, $extensions = array() ) { 
    if ( empty( $extensions ) || ! is_array( $extensions ) || ! is_dir( $dir ) ) return false;
    foreach ( $extensions as $ext ) if ( count( glob( $dir . '/*.' . $ext ) ) > 0 ) $found[$ext] = 1;
    return ( count( $found ) == count( $extensions ) ) ? true : false;
}
// use it like
$dir = dirname(__FILE__) . '/images'; //absolute path to the directory you wanna test
$extensions = array('jpg','png'); // an array containing the file extensions you seek for
var_dump( has_the_files( $dir, $extensions ) ); // outputs: bool(false)  
使用

GLOB_BRACE 时,您必须使用括号{} 。以下代码应该有效:

var_dump(glob($video_dir.'{*.txt,*.jpg}', GLOB_BRACE));