如何在PHP中回显.html文件的全部内容?

2022-08-30 09:19:50

有没有办法在PHP中回显.html文件的全部内容?

例如,我有一些示例.html文件,并且我想回显该文件名,因此应该显示其内容。


答案 1

你应该使用readfile()

readfile("/path/to/file");

这将读取文件并通过一个命令将其发送到浏览器。这基本上与以下相同:

echo file_get_contents("/path/to/file");

除了file_get_contents() 可能会导致脚本崩溃,而对于大文件,则不会。readfile()


答案 2

只需使用:

<?php
    include("/path/to/file.html");
?>

这也将与它相呼应。这还具有在文件中执行任何PHP的好处。

如果您需要对内容执行任何操作,请使用 file_get_contents(),

例如

<?php
    $pagecontents = file_get_contents("/path/to/file.html");

    echo str_replace("Banana", "Pineapple", $pagecontents);

?>

这不会执行该文件中的代码,因此,如果您希望它有效,请小心。

我通常使用:

include($_SERVER['DOCUMENT_ROOT']."/path/to/file/as/in/url.html");

因为这样我就可以移动文件而不会破坏包含。


推荐