简体   繁体   English

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

[英]PHP Check if dynamic named folder exists

I'm having problems checking if a dinamically named folder exists using php. 我在检查是否存在使用php命名的文件夹时遇到问题。 I know i can use 我知道我可以用

file_exists()

to check if a folder or file exists, but the problem is that the name of the folders I'm checking may vary. 检查文件夹或文件是否存在,但是问题是我要检查的文件夹名称可能有所不同。

I have folders where the first part of the name is fixed, and the part after "_" can vary. 我有一些文件夹,其中名称的第一部分是固定的,“ _”之后的部分可以有所不同。 As an example: folder_0, where every folder will start with "folder_" but after the "_" it can be anything. 例如:folder_0,其中每个文件夹都以“ folder_”开头,但在“ _”之后可以是任何东西。

Anyway i can check if a folder with this property exists? 无论如何,我可以检查是否存在具有此属性的文件夹?

Thanks in advance. 提前致谢。 SR SR

You make a loop to go through all the files/folders in the parent folder: 您循环浏览父文件夹中的所有文件/文件夹:

$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');

}

eg 例如

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

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

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