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

Nik*_*ski 49 html php echo

有什么方法可以回应php中的.html文件的全部内容吗?

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

Frx*_*rem 84

你应该使用readfile():

readfile("/path/to/file");
Run Code Online (Sandbox Code Playgroud)

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

echo file_get_contents("/path/to/file");
Run Code Online (Sandbox Code Playgroud)

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

  • 你使用`echo readfile()`吗?因为 `readfile()` 返回文件的长度(似乎是 1191 字节),如果你在 `readfile()` 之前添加 `echo`,它会在你打印文件之后*打印长度。只需删除`echo`,它应该可以工作。:) (2认同)

Ric*_*haw 14

只需使用:

<?php
    include("/path/to/file.html");
?>
Run Code Online (Sandbox Code Playgroud)

这也将回应它.这也有利于在文件中执行任何PHP,

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

例如

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

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

?>
Run Code Online (Sandbox Code Playgroud)

这不会执行该文件中的代码,因此如果您希望这样做,请小心.

我通常使用:

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

因为那时我可以在不破坏包含的情况下移动文件.


Not*_*fer 8

如果你想确保HTML文件不包含任何PHP代码并且不会以PHP形式执行,请不要使用includerequire,简单地执行:

echo file_get_contents("/path/to/file.html");
Run Code Online (Sandbox Code Playgroud)