How do i get the source code of a php file hosted on the same server through php code?

Something like this to echo the source code of the php file named file.php.

echo readfile("/var/html/rootpath.com/file.php");

Thanks for any help towards this, i tried file_get_contents but that just gets the html source and not the php file's source code.

有帮助吗?

解决方案

Make use of fgets()

<?php
$handle = fopen('test.php','r+');
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo htmlentities($buffer);
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

Assuming your test.php contains a code of

<?php
echo "Hello World";
?>

其他提示

echo file_get_contents("/var/html/rootpath.com/file.php");

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top