繁体   English   中英

PHP检查动态命名文件夹是否存在

[英]PHP Check if dynamic named folder exists

我在检查是否存在使用php命名的文件夹时遇到问题。 我知道我可以用

file_exists()

检查文件夹或文件是否存在,但是问题是我要检查的文件夹名称可能有所不同。

我有一些文件夹,其中名称的第一部分是固定的,“ _”之后的部分可以有所不同。 例如:folder_0,其中每个文件夹都以“ folder_”开头,但在“ _”之后可以是任何东西。

无论如何,我可以检查是否存在具有此属性的文件夹?

提前致谢。 SR

您循环浏览父文件夹中的所有文件/文件夹:

$folder = '/path-to-your-folder-having-subfolders';
$handle = opendir($folder) or die('Could not open folder.'); 
while (false !== ($file = readdir($handle))) { 
    if (preg_match("|^folder_(.*)$|", $file, $match)) {
        $curr_foldername = $match[0];
        // If you come here you know that such a folder exists and the full name is in the above variable
    }
}
function find_wildcard_dirs($prefix)
{
   return array_filter(glob($prefix), 'is_dir');

}

例如

print_r(find_wildcard_dirs("/tmp/santos_*'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM