How to read a .php file using php
Let's say you have two files a.php
and b.php
on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php
on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php
is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
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.
//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