3

I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).

I tried using $html = file_get_content("B590.php");

But then $html will have the content of B90.php as plain text(with php code).

Is there any method where I can get the content of the file after it has been evaluated? There seems to be many related questions like this one and this one but none seems to have any definite answer.

1
  • 1
    Shouldn't it be file_get_contents('B590.php');, with an s at the end? probably a typo, but still... Oct 23, 2012 at 10:41

5 Answers 5

5

You can use include() to execute the PHP file and output buffering to capture its output:

ob_start();
include('B590.php');
$content = ob_get_clean();
0
3
    function get_include_contents($filename){
      if(is_file($filename)){
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
      }
      return false;
    }

    $html = get_include_contents("/playbooks/html_pdf/B580.php");

This answer was originally posted on Stackoverflow

2
  • Do you have a reference to the "original" post - I found this extremely useful and would like to share the love :)
    – kwah
    Aug 16, 2013 at 17:48
  • 1
    dont actually remember...but i think it was this stackoverflow.com/questions/6688343/…
    – antnewbee
    Aug 30, 2013 at 6:23
1

If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:

ob_start();
include('B590.php');
$html = ob_get_clean();

Example:

B590.php

<div><?php echo 'Foobar'; ?></div>

current.php

$stuff = 'do stuff here';
echo $stuff;
include('B590.php');

will output:

do stuff here
<div>Foobar</div>

Whereas, if current.php looks like this:

$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;

The output will be:

do stuff here
Some more
<div>Foobar</div>

1

To store evaluated result into some variable, try this:

ob_start();
include("B590.php");
$html = ob_get_clean();
0
$filename = 'B590.php';
$content = '';

if (php_check_syntax($filename)) {
    ob_start();
    include($filename);
    $content = ob_get_clean();
    ob_end_clean();
}

echo $content;
2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.