2012-05-10 168 views
1

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

这是我目前的代码,这是行不通的。如果我只用一个文件类型它的工作原理,但不会返回一个错误:

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

感谢

+2

你能后至今你已经尝试请的代码? – PeeHaa

回答

4
// 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) 
1

当使用GLOB_BRACE你必须使用括号{}。下面的代码应该工作:

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

好的。但我想要一个错误的返回,如果其中一种文件类型丢失。 – user998163

+0

好吧,我明白了。我想,如果不循环你的扩展并使用'glob()'来找到它们,这是不可能的。 – mixable

相关问题