Question

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.

Was it helpful?

Solution

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";
?>

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top