沃梦达 / 编程问答 / php问题 / 正文

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

PHP - Iterate through folders and display HTML contents(PHP - 遍历文件夹并显示 HTML 内容)

本文介绍了PHP - 遍历文件夹并显示 HTML 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试开发一种方法来概览我多年来创建和(合法地)下载的所有不同 Web 模板.我想过像 WordPress 那样显示它们,正在用一个小的预览窗口预览它的模板,显示具体的包含样式和所有内容的文件.

I'm currently trying to develop a method to get a overview of all my different web templates I've created and (legally) downloaded over the years. I thought about displaying them like WordPress is previewing its templates with a small preview window, displaying the concrete file with styles and everything.

如何将它们分成行和列并创建 Ajax 模态预览和分页等窗口打开?

How do I divide them into rows and columns and create Ajax modal window open on preview and pagination and so on?

我相信我可以管理,但它是关于迭代多个文件夹然后找到所有 index.htmindex.html 页面并显示它们的概念本身.

I believe I can manage, but it is the concept itself about iterating over several folders and then finding all index.htm and index.html pages and displaying them.

我在 PHP 中的目录工作不多,到目前为止我发现的唯一引用和代码树桩只是列出某个目录中的所有文件,比如它包含的内容.

I've not worked very much with directories in PHP and the only references and code stumps I've found so far is just to list all the files in a certain directory like, what it contains.

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

Is there a script, a function, snippet or just some information to create such a (probably simple) preview function?

推荐答案

glob('*.html') 如果它们都在一个目录中,则将起作用.

glob('*.html') will work if they're all in one directory.

如果您想遍历文件树——检查当前目录和子目录以及子目录的子目录(等)中的所有内容——那么您有几个选择.

If you want to walk a file tree -- checking everything in the current directory and in subdirectories and subdirectories of subdirectories (etc) -- then you have a couple of options.

一种方法是使用 unix find 命令和 PHP 系统调用的方法之一.比如:

One would be to use the unix find command with one of the methods of PHP system invocation. Something like:

找到 <search_root_dir>-name "*.html" -print

find <search_root_dir> -name "*.html" -print

会得到类似的输出

search_root_dir/blah.html
search_root_dir/foo.html
search_root_dir/subdir/baz.html
search_root_dir/subdir/bah.html
...

您可以做的另一件事是编写一个使用 chdirreaddirscandir 的递归函数,类似于:

Another thing you could do is write a recursive function that uses chdir and readdir or maybe scandir, something like:

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);
    }
}

然后,编写另一个函数:

Then, write another function:

$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 以便它接受一个带有您可以在内部进行的方法调用的对象.这里可能有一些变化.

Or, write dir_walk so it accepts an object with a method call you can make inside. There's some variations possible here.

这篇关于PHP - 遍历文件夹并显示 HTML 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP - 遍历文件夹并显示 HTML 内容

基础教程推荐