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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9539849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:03:27  来源:igfitidea点击:

How to echo the whole content of an .html file in php?

phphtmlecho

提问by Nikola Nastevski

Is there any way I can echo the whole content of an .html file in php?

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

For e.g. I have some sample.html file, and I want to echo that filename, so its content should be displayed.

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

回答by Frxstrem

You should use readfile():

你应该使用readfile()

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

This will read the file and send it to the browser in one command. This is essentially the same as:

这将读取文件并将其发送到浏览器中的一个命令。这与以下内容基本相同:

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

except that file_get_contents()may cause the script to crash for large files, while readfile()won't.

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

回答by Rich Bradshaw

Just use:

只需使用:

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

That will echo it as well. This also has the benefit of executing any PHP in the file,

这也将呼应它。这也有利于执行文件中的任何 PHP,

If you need to do anything with the contents, use file_get_contents(),

如果您需要对内容做任何事情,请使用 file_get_contents(),

e.g.

例如

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

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

?>

This doesn't execute code in that file, so be careful if you expect that to work.

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

I usually use:

我通常使用:

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

as then I can move files without breaking the includes.

这样我就可以在不破坏包含的情况下移动文件。

回答by Not_a_Golfer

If you want to make sure the HTML file doesn't contain any PHP code and will not be executed as PHP, DO NOT use includeor require, simple do:

如果您想确保 HTML 文件不包含任何 PHP 代码并且不会作为 PHP 执行,请不要使用includerequire,简单地做:

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