-1

Lets say I want to use a php script to read the current page's code into a string how would I do that without executing the php contents of the page?

For example using

file_get_contents("test.php");

Will get the contents of test.php but as it does it will execute the code... How can I just get the source code of the file?

2
  • That should work. It won't execute php file. Commented Mar 16, 2012 at 1:46
  • Actually, no, file_get_contents won't execute the file. Not unless it's on a remote host, in which case what you ask is impossible. Commented Mar 16, 2012 at 1:46

2 Answers 2

0

file_get_contents() will only execute that file and give you the output as the content if you refer to it as a URL. If you just give a filename, it will attempt to read it from (in your case) the current directory.

Sign up to request clarification or add additional context in comments.

3 Comments

How can I call it? I want to run a html file which calls a php file to read the contents of the first file (html) to get it's source into a string. I am using: file_get_contents("example.com/".basename($_SERVER['SCRIPT_NAME']) Which is a url right?
So wait, do you want to execute the code of test.php or not?
I didn't but I was using the full URL instead of the relative location and it was executing the code, I changed it and now it works using a relative path. Thanks!
0

Actually there is one way to execute PHP file and get the content into a variable.

You can use the ob_start() and ob_get_clean() functions.

ob_start();
include "teste.php";
$String = ob_get_clean();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.