PHP - 遍历文件夹并显示HTML内容 [英] PHP - Iterate through folders and display HTML contents
问题描述
我目前正在尝试开发一种方法来概述我多年来创建和(合法)下载的所有不同的网页模板。我想要显示它们像 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.htm
和 index.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.
是否有脚本,函数,代码段或只是一些信息来创建这样一个(可能是简单的)previ ew函数?
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:
find< 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
...
你能做的另一件事就是编写一个使用 chdir $的递归函数c $ c>和
readdir
或者 scandir
,类似于:
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内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!