2

I have a PHP script that I want to run on the command line. This script, among other things, needs to load a PHP file that contains both PHP and HTML content and get the rendered output from that file.

This code does exactly what I need, but not when run from the command line:

<?php

// ...
if(file_exists($content_file)) {
  ob_start();
  include($content_file);
  $content = ob_get_contents();
  ob_end_clean();
}

?>

When run in the browser, my script gets the rendered output of the PHP file via include(), and stores the output in $content.

However, when I execute this script on the command line, the contents of the PHP file are echoed out, and $content never gets set.

I've searched the documentation, but nothing seems to work. Calling ini_set('implicit_flush', false) has no effect, nor does ob_implicit_flush(0);

Any thoughts?

4
  • Try to surpress errors by adding a @ before the include: @include($content_file); Commented Apr 5, 2011 at 18:32
  • 6
    @Raisen, there are no errors here, and using the @ error silencing operator is generally considered to be a bad practice. Commented Apr 5, 2011 at 18:34
  • Can you include the command you're using to call the script? I know it's a long shot, but that may affect your output. Commented Apr 5, 2011 at 18:36
  • From the unlikely-but-possible-error-dept.: try ob_start(NULL,0) and also ensure that no ob_flush() exists in your include file. Commented Apr 5, 2011 at 18:40

1 Answer 1

1

I am unable to duplicate this problem using PHP 5.3.

[charles@lobotomy ~]$ cat includeme.php
<?php

echo "Oh hi!\n";
?>
I am an include!

[charles@lobotomy ~]$ cat includehim.php
<?php

$content_file = './includeme.php';

if(file_exists($content_file)) {
  ob_start();
  include($content_file);
  $content = ob_get_contents();
  ob_end_clean();
}
[charles@lobotomy ~]$ php includehim.php
[charles@lobotomy ~]$

And at the interactive prompt:

[charles@lobotomy ~]$ php -a
Interactive shell

php > $content_file = './includeme.php';
php > if(file_exists($content_file)) {
php {   ob_start();
php {   include($content_file);
php {   $content = ob_get_contents();
php {   ob_end_clean();
php { }
php >
php > echo $content;
Oh hi!
I am an include!

php > exit;
Sign up to request clarification or add additional context in comments.

3 Comments

I have exactly the same problem using PHP5.3 - CakePHP2.0 and Shells seem to ignore the ob_start() etc. Maybe because the shell is running on a CLI level or so? In my case I want to catch the result of "system('dos2unix -h', $x);" but it always prints it right out...
@mark, you probably want to use exec or proc_open instead of system. system intentionally screws with the output buffer.
thx. I also used exec without success. the strange thing is: exec('svn/git') works. the output is not flushed but returned properly. proc_open i have never heard of. will give it a try!

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.