【问题标题】:php html files within directorys within a directory目录中的目录中的 php html 文件
【发布时间】:2017-07-05 10:04:54
【问题描述】:

我正在尝试在根目录中的目录中查找所有 html 文件,我有一个包含所有目录的数组,我正在尝试遍历数组中的每个目录以查找文件并创建一个列表,但它不工作,有什么想法吗?

$dirs = glob("/Root_directory*", GLOB_ONLYDIR);

foreach($dirs as $dir) {
    $phpfiles = glob($dir . "*.html");

    foreach($phpfiles as $phpfile) {
        echo "<a href=$phpfile>".basename($phpfile)."</a>";
    }
}

我还尝试了推荐的方法here 使用递归,这给出了一个空数组

$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

【问题讨论】:

  • “它不工作”不是一个很明显的问题。你能解释一下你希望你的代码做什么以及它做了什么吗?
  • 我希望它生成指向主目录中每个目录中所有 html 文件的超链接,但是当 php 代码运行时它什么也没有生成
  • 另外,对于这种问题,你应该看看递归编程。 en.wikipedia.org/wiki/Recursion_(computer_science)
  • 我查看了一些递归答案,例如 this,但这对于查找特定文件类型不起作用

标签: php html directory glob


【解决方案1】:

这是经过大量摆弄后起作用的递归方法。

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),        RecursiveIteratorIterator::SELF_FIRST);
$Regex = new RegexIterator($objects, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH);
foreach($Regex as $name => $Rege){
echo "<a href=$name>".$name."</a> \n";
}

【讨论】:

    猜你喜欢
    • 2010-09-23
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    相关资源
    最近更新 更多