我有一个php页面(index.php),其中包含以下内容:
<php
include("{$_SERVER['DOCUMENT_ROOT']}/somefunction.php")
echo "hello world";
?>
现在我想通过另一个php文件(test.php)读取index.php的内容我想要得到的结果是:
line 1: include("{$_SERVER['DOCUMENT_ROOT']}/somefunction.php")
line 2: echo "hello world";
这是我已经尝试过的,但不起作用:
$phppage="{$_SERVER['DOCUMENT_ROOT']}/index.php";
$handle = fopen($phppage, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
echo $line;
}
fclose($handle);
}
else {
// error opening the file.
echo "An error occured";
}
它只是在回响一根空弦。
问题已解决:-)
实际上您的代码正在工作。问题是它不会显示在浏览器上。由于它包含类似<
的html特殊字符
因此,在<pre> </pre>
标记中打印带有的结果。
或使用htmlentities()
echo htmlentities($line);
或从文件index.php 中删除<?php ?>
当你说"它不起作用"时,你会收到任何错误消息吗?您是否尝试打开error_reporting并显示错误?
这可能与安全有关-没有打开文件的权限。
尝试使用相对路径,并仔细检查是否存在basedir限制。
你在找这个吗。。。
show_source()
此函数在浏览器中显示PHP代码。。
网址:http://www.w3schools.com/php/func_misc_show_source.asp
this is working try this
read.php as
<?php
$phppage=realpath("index.php");
$data = htmlentities(file_get_contents($phppage));
echo $data;
?>
index.php as
<?php
$absolute_path = realpath("somefunction.php");
include($absolute_path);
echo "hello world";
?>
如果您不需要行号,而这些行号不是由您的代码生成的,您可以执行以下操作:
readfile('index.php');
或
echo file_get_contents('index.php');
只要你在同一个目录中工作,它就会起作用。如果您需要行号:
$lines = explode("'n",file_get_contents('index.php'));
foreach ($lines as $key => $line) echo "line {$key+1}: $line'n";
如果你不想回声,那是显而易见的。