Read a .php file using php

2019-02-19 18:26发布

问题:

How to read a .php file using php

回答1:

Lets say you have files a.php and b.php on same folder.

Code on b.php

<?php

echo "hi";

?>

code on a.php

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

What you see? A blank page. Check page source. It is there. Not showing in browser as <?php is not a valid html tag.

<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;

Now you can see that.



回答2:

If you want to get the content generated by PHP, then

$data = file_get_contents('http://host/path/file.php');

If you want to get the source code of the PHP file, then

$data = file_get_contents('path/file.php');

Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.



回答3:

//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");

//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);

Each line of the 'myfilename.php' will be stored as a string in the array '$lines'. And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php