Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-遍历文件夹并显示HTML内容_Php_Wordpress_Iteration_Loops - Fatal编程技术网

PHP-遍历文件夹并显示HTML内容

PHP-遍历文件夹并显示HTML内容,php,wordpress,iteration,loops,Php,Wordpress,Iteration,Loops,我目前正在尝试开发一种方法,以获得多年来我创建和(合法)下载的所有不同web模板的概述。我想显示它们就像是用一个小预览窗口预览它的模板,显示带有样式和所有内容的具体文件 如何将它们划分为行和列,并创建模式窗口(在预览和分页时打开),等等 我相信我能做到,但这是一个概念,它本身就是迭代几个文件夹,然后找到所有index.htm和index.html页面并显示它们 我在PHP中并没有使用过很多目录,到目前为止,我发现的唯一引用和代码树桩就是列出某个目录中的所有文件,比如它包含的内容 是否有脚本、函数

我目前正在尝试开发一种方法,以获得多年来我创建和(合法)下载的所有不同web模板的概述。我想显示它们就像是用一个小预览窗口预览它的模板,显示带有样式和所有内容的具体文件

如何将它们划分为行和列,并创建模式窗口(在预览和分页时打开),等等

我相信我能做到,但这是一个概念,它本身就是迭代几个文件夹,然后找到所有
index.htm
index.html
页面并显示它们

我在PHP中并没有使用过很多目录,到目前为止,我发现的唯一引用和代码树桩就是列出某个目录中的所有文件,比如它包含的内容

是否有脚本、函数、代码片段或只是一些信息来创建这样一个(可能很简单的)预览函数?

glob('*.html')
如果它们都在一个目录中,则可以工作

如果要遍历文件树--检查当前目录、子目录和子目录的子目录(等等)中的所有内容--那么有几个选项

一种方法是将unix
find
命令与PHP系统调用方法之一结合使用。比如:

function dir_walk($start_dir,$func) {
    $entries = scandir($start_dir);
    foreach($entries as $entry) {
        if($entry == '.' || $entry == '..') {
            /*skip these*/
        } else if(is_dir($entry)) {
            dir_walk($start_dir.'/'.$entry,$func);
        } else $func($start_dir.'/'.$entry);
    }
}
find-name“*.html”-打印

将得到类似于的输出

search_root_dir/blah.html
search_root_dir/foo.html
search_root_dir/subdir/baz.html
search_root_dir/subdir/bah.html
...
您可以做的另一件事是编写一个递归函数,使用
chdir
readdir
或者
scandir
,类似于:

function dir_walk($start_dir,$func) {
    $entries = scandir($start_dir);
    foreach($entries as $entry) {
        if($entry == '.' || $entry == '..') {
            /*skip these*/
        } else if(is_dir($entry)) {
            dir_walk($start_dir.'/'.$entry,$func);
        } else $func($start_dir.'/'.$entry);
    }
}
然后,编写另一个函数:

$html_files = array();
function record_html_files($filename) {
   global $html_files;
   if(strpos($filename,'*.html') === (strlen($filename) - 6))
     $html_files[] = $filename;
}
这样称呼它:

dir_walk('/path/to/search/root','record_html_files');
dir_walk('/path/to/search/root','record_html_files'); 或者,编写dir_walk,这样它就可以接受一个对象,并在其中进行方法调用。这里可能有一些变体。

glob('*.html')
如果它们都在一个目录中,则可以使用

如果要遍历文件树--检查当前目录、子目录和子目录的子目录(等等)中的所有内容--那么有几个选项

一种方法是将unix
find
命令与PHP系统调用方法之一结合使用。比如:

function dir_walk($start_dir,$func) {
    $entries = scandir($start_dir);
    foreach($entries as $entry) {
        if($entry == '.' || $entry == '..') {
            /*skip these*/
        } else if(is_dir($entry)) {
            dir_walk($start_dir.'/'.$entry,$func);
        } else $func($start_dir.'/'.$entry);
    }
}
find-name“*.html”-打印

将得到类似于的输出

search_root_dir/blah.html
search_root_dir/foo.html
search_root_dir/subdir/baz.html
search_root_dir/subdir/bah.html
...
您可以做的另一件事是编写一个递归函数,使用
chdir
readdir
或者
scandir
,类似于:

function dir_walk($start_dir,$func) {
    $entries = scandir($start_dir);
    foreach($entries as $entry) {
        if($entry == '.' || $entry == '..') {
            /*skip these*/
        } else if(is_dir($entry)) {
            dir_walk($start_dir.'/'.$entry,$func);
        } else $func($start_dir.'/'.$entry);
    }
}
然后,编写另一个函数:

$html_files = array();
function record_html_files($filename) {
   global $html_files;
   if(strpos($filename,'*.html') === (strlen($filename) - 6))
     $html_files[] = $filename;
}
这样称呼它:

dir_walk('/path/to/search/root','record_html_files');
dir_walk('/path/to/search/root','record_html_files');
或者,编写dir_walk,这样它就可以接受一个对象,并在其中进行方法调用。这里可能有一些变体。

如果您能够找到代码示例来迭代某个目录并列出其内容,我觉得您唯一缺少的就是将其过滤为HTML文件。你到底被困在哪里了?如果你能找到代码样本来迭代一个目录并列出它的内容,我觉得你唯一缺少的就是将它过滤成HTML文件。你到底被困在哪里了?