2012-05-10 228 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

相關問題