用PHP回显多个html文件的内容

用PHP回显多个html文件的内容,php,html,Php,Html,我尝试回显特定目录中每个html文件的全部内容 <?php function read_htmlfiles_in_dir($dir) { $files = array(); if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." &


我尝试回显特定目录中每个html文件的全部内容

<?php
function read_htmlfiles_in_dir($dir)
{
    $files = array();
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle)))
        {
            if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'html')
            {
                $files[$file] = file_get_contents($file);
            }
        }
        closedir($handle);
        return ($files);
    }
    else
    {
        return (false);
    }
}
?>
我知道我可以使用
readfile()
读取并返回特定文件的内容。如何将其用于特定目录中的每个html文件


谢谢你的帮助

您可以使用
file\u get\u contents()
存储文件的内容

此函数(受启发)将返回一个2D数组,其中
$key=>$value
对由
$filename=>$file\u content
表示,如果未能打开目录,则返回
false

<?php
function read_htmlfiles_in_dir($dir)
{
    $files = array();
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle)))
        {
            if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'html')
            {
                $files[$file] = file_get_contents($file);
            }
        }
        closedir($handle);
        return ($files);
    }
    else
    {
        return (false);
    }
}
?>
当然你需要一点调试你只是

print_r($htmlfiles);
然后得到类似于

Array
(
    [filename.html] => <div>Some HTML Code</div>
    [otherfile.html] => <span>Some <b>other</b> HTML code</span>

)
数组
(
[filename.html]=>一些html代码
[otherfile.html]=>其他一些html代码
)

不过,您还有最后一件事要考虑:HTML文件的显示顺序。

或者您可以做一些更简单的事情

$FILES = scandir("/complete/path/to/directory");
foreach($FILES as $FILE)
    if($FILE == "." || $FILE == "..") continue;
    else include($FILE);
如果它是当前目录

$FILES = scandir(realpath(dirname(__FILE__)));
foreach($FILES as $FILE)
        if($FILE == "." || $FILE == ".." || $FILE ==  __FILE__) continue;
        else include($FILE);

读取目录以获取其中的所有文件名。迭代它们,打开阅读它们,并响应它们。点击php.net无需打开/读取/回显。。只要包含,如果你认为我们只需要回应HTML内容,无论订单。如果有一个订单,我想他会手动,而不是循环每一个。