我正在使用 RecursiveDirectoryIterator()。我有一种情况,我想向我的程序发出信号,应该在目录层次结构中指定字符串的第一个实例上创建一个文件。我希望 RecursiveDirectoryIterator() 适合这个,但如果有另一个更有意义的选择,我也愿意接受。
有关我的问题的详细信息,请参见下文...谢谢。
代码:
$in_dir = 'none';
$path = '.';
$dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $object){
//if this is a directory... find out which one it is.
if($object->isDir()){
if(strpos($object->getPathname(), 'dir1') == true){
//i need a file (file1.txt) created here, upon the first instance found
$in_dir = 'dir1';
}else if(strpos($object->getPathname(), 'dir2') == true){
//i need a file (file2.txt) created here, upon the first instance found
$in_dir = 'dir2';
}
}