有一个充满随机文件夹的目录,所有这些文件夹中都有name.txt
示例:abcd / name.txt,1234 / name.txt,xyz / name.txt
<?php $users_input = "Jonathan"; ?>
我想检查$ users_input是否等于任意随机文件夹name.txt和echo THAT 文件夹名称的内容。
like(如果$ users_input等于1234 / name.text的内容){echo文件夹的名称 1234 }
答案 0 :(得分:0)
您可以使用RecursiveDirectoryIterator来实现此目的,如下所示:
$users_input = 'Jonathan';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/target/path'));
foreach ($iterator as $file)
{
if ($file->isDir()) {
continue;
}
$path = $file->getPathname();
$contents = file_get_contents($path);
if (strcmp($contents,$users_input)) {
echo $path.'<br\>';
}
}
如果文件夹可以包含具有不同名称和/或扩展名的其他文件,您可能需要对文件名进行额外检查。
答案 1 :(得分:0)
根据Barmar的建议,解决方案的外观如下:
<?php
$users_input = "Jonathan";
$file_arr = glob("*/name.txt");
foreach ($file_arr as $path) {
$content = file_get_contents($path);
if (trim($content) === $users_input) {
echo "dirname: " . dirname($path) . "\n";
}
}
答案 2 :(得分:0)
谢谢大家。
结束
$users_input = "Jonathan";
$directory = glob("*");
foreach ($directory as $path) {
if (is_dir($path)) {
$myfile = fopen($path."/name.txt", "r") or die("Unable to open file!");
if (fread($myfile,filesize($path."/name.txt")) == $users_input) {echo $users_input . " is in " . $path;}
fclose($myfile);
}
}