获取php文件的内容

时间:2010-11-16 21:24:36

标签: php file get

如何从其他php文件中的一个php文件中获取所有当前代码?

就像,我们在文件a.php里面有一些函数。

文件b.php,我们希望从a.php获取所有代码(不运行任何函数),并在请求时将其回显给浏览器。

(在b.php内):

$content = get_all_file_text(a.php);
echo $content;

感谢。

8 个答案:

答案 0 :(得分:8)

似乎每个人都喜欢file_get_contents,当我尝试将其输出到浏览器时,这是我的建议:

file_get_contents + highlight_string

http://php.net/manual/en/function.highlight-string.php

答案 1 :(得分:3)

这看起来很简单,但不幸的是浏览器会将<?php解释为HTML标记的开头。

为避免这种情况,您必须在PHP代码上使用htmlentities。另外,为了保留格式,nl2br函数。

c.php:

<?php
$f='snizz';
function plip($f){ echo $f; }
$r=array(1,2,3,32);

a.php只会:

<?php
$f=file_get_contents('c.php');
echo nl2br(htmlentities($f));

这将在浏览器中生成正确的输出。

我也喜欢使用highlight_string函数的答案。

答案 2 :(得分:2)

请参阅此方法:

http://de2.php.net/manual/en/function.file-get-contents.php

这会让你回到一个字符串,你可以很容易地给你的客户。

美好的一天。

答案 3 :(得分:2)

查看此页面:

http://de2.php.net/manual/en/function.file-get-contents.php

以下是一个例子:

<?php
$file = file_get_contents('b.php');
echo $file;
?>

答案 4 :(得分:1)

我相信你正在寻找file_get_contents()函数。查看www.php.net/file_get_contents

答案 5 :(得分:0)

答案 6 :(得分:0)

readfile将比file_get_contents

更简单

readfile读取文件并将其输出到一个函数中,而file_get_contents将其读入一个必须输出的字符串

php.net/readfile

答案 7 :(得分:0)

请检查此代码......

a.php只会

<?php
echo file_get_contents("http://localhost/yourpath/b.php");
?>

b.php

<?php
 echo "HELLO WORLD!!!";
?>