php 包含文件的全部内容并回显它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/921479/
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
Include whole content of a file and echo it
提问by Elliott
I need to echo entire content of included file. I have tried the below:
我需要回显包含文件的全部内容。我尝试了以下方法:
echo "<?php include ('http://www.example.com/script.php'); ?>";
echo "include (\"http://www.example.com/script.php\");";
But neither works? Does PHP support this?
但两者都不起作用?PHP 支持这个吗?
回答by ceejayoz
Just do:
做就是了:
include("http://www.mysite.com/script.php");
Or:
或者:
echo file_get_contents("http://www.mysite.com/script.php");
Notes:
笔记:
- This may slow down your page due to network latency or if the other server is slow.
- This requires
allow_url_fopento be on for your PHP installation. Some hosts turn it off. - This will not give you the PHP code, it'll give you the HTML/text output.
- 由于网络延迟或其他服务器速度较慢,这可能会减慢您的页面速度。
- 这需要
allow_url_fopen为您的 PHP 安装打开。一些主机将其关闭。 - 这不会给你 PHP 代码,它会给你 HTML/文本输出。
回答by Matt
Shortest way is:
最短的方法是:
readfile('http://www.mysite.com/script.php');
That will directly output the file.
那将直接输出文件。
回答by Lou Morda
This may not be the exact answer to your question, but why don't you just close the echo statement, insert your include statement, and then add a new echo statement?
这可能不是您问题的确切答案,但是您为什么不关闭 echo 语句,插入您的 include 语句,然后添加一个新的 echo 语句?
<?php
echo 'The brown cow';
include './script.php';
echo 'jumped over the fence.';
?>
回答by Adam Wright
Echo prints something to the output buffer - it's not parsed by PHP. If you want to include something, just do it
Echo 将一些内容打印到输出缓冲区 - 它没有被 PHP 解析。如果你想包括一些东西,就去做吧
include ('http://www.mysite.com/script.php');
You don't need to print out PHP source code, when you're writing PHP source code.
在编写 PHP 源代码时,您不需要打印出 PHP 源代码。
回答by Pavel Lishin
Not really sure what you're asking, but you can't really include something via http and expect to see code, since the server will parse the file.
不太确定你在问什么,但你不能真正通过 http 包含一些东西并期望看到代码,因为服务器会解析文件。
If "script.php" is a local file, you could try something like:
如果“script.php”是本地文件,您可以尝试以下操作:
$file = file_get_contents('script.php');
echo $file;
回答by techbio
Matt is correct with readfile()but it also may be helpful for someone to look into the PHP file handling functions
manual entry for fpassthru
Matt 是正确的,readfile()但它也可能有助于某人查看fpassthru的 PHP 文件处理函数
手册条目
<?php
$f = fopen($filepath, 'r');
fpassthru($f);
fclose($f);
?>

