File_get_contents and php code

Hi, is there a way to take the contents of a php file while processing PHP code? I need to include the code on another page, but I need to print the content in a specific position.

If I use include, the code will be printed before the html tag, of course, causes it to be processed before the page, but if I use file_get_contents, the content will be shown on the page, but if I have some php tags, I will also get them as plain text.

Thanks.

EDIT: sorry guys, it looks like I was drunk when I wrote. I have corrected.

I have a engine that processes the contents of a page, puts them in a variable, and then prints them at a specific position on the html page. In the engine, I need to "embed" the code of other "static" pages, which may have some php tags. If I use file_get_contents, I will get the content in plain text (with php tags that are not processed), if I use include, it simply will not work, because this is not a function for it. Therefore, I need to insert the PROCESSED code into the engine (as HTML ready for printing).

+6
include php
source share
3 answers

You can use the output buffer to include the PHP file, but save the result for later use, and not immediately print it in the browser.

ob_start(); include('path/to/file.php'); $output = ob_get_contents(); ob_end_clean(); // now do something with $output 
+22
source share

Your question is not 100% understandable, but it looks like you want to enable the remote php source, you can use include ("http://domain.com/path/to/php.phps"); instead of file_get_contents.

You said that you just need a piece of remote code, if so, you can use file_get_contents, get that piece of code with substr or reg exp in a local file and enable or do eval.

None of the solutions are safe or recommended; you should never include deleted files to directly evaluate your code.

-one
source share

If I understand correctly; you are trying to download a php file to read its contents and execute it at the same time.

namely: include('/foo/bar') as well as file_get_contents('/foo/bar')

The following function performs both actions and returns the contents of the file, assuming that it exists.

 function load_and_process($path) { if(file_exists($path) { include($path); return file_get_contents($path); } return false; } 

and name it like this:

 if($file_contents = load_and_process('/foo/bar')) { echo "Loaded $file_contents from /foo/bar"; } 
-one
source share

All Articles