문제

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