Search code examples
phpremote-file-inclusion

Use php to read the source of a php file


I'm attempting to use php to read the source of a separate php file. I'm attempting to use file_get_contents in the following manner

file_get_contents('http://www.example.com/someFile.php');

Unfortunately, the code above attempts to execute the php code rather than just reading the text as it would with any other file.

I came across an article which appears to address the issue, which led me to the following code:

file_get_contents('php://filter/convert.base64-encode/resource=http://www.example.com/someFile.php');

However this code has the same effect; It attempts to execute the php code.

How can I use php to read the source of another php file without attempting to execute the php in the file?


Solution

  • Unfortunately, the code above attempts to execute the php code rather than just reading the text as it would with any other file.

    No, it doesn't.

    What's happening here is that file_get_contents makes a regular HTTP request for http://www.example.com/someFile.php, and the remote server at "example.com" is interpreting the PHP code. It serves the results up exactly as though you'd navigated to http://www.example.com/someFile.php in your browser. Your script is downloading that output.

    file_get_contents most definitely does not execute the contents of the file after retrieving it. The only access your script has to "someFile.php" is what the remote server is willing to serve up; file_get_contents cannot somehow fetch the underlying PHP source any more than you could with your browser somehow view the PHP source.