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?

Erich
  • 29
  • 1
  • 2
  • Try to surpress errors by adding a @ before the include: @include($content_file); – Raisen Apr 05 '11 at 18:32
  • 6
    @Raisen, there are no errors here, and using the `@` error silencing operator is generally considered to be a bad practice. – Charles Apr 05 '11 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. – eykanal Apr 05 '11 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. – mario Apr 05 '11 at 18:40

1 Answers1

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;
Charles
  • 50,943
  • 13
  • 104
  • 142
  • 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 Nov 04 '11 at 20:12
  • @mark, you probably want to use [`exec`](http://php.net/exec) or [`proc_open`](http://php.net/proc_open) instead of [`system`](http://php.net/system). `system` *intentionally* screws with the output buffer. – Charles Nov 04 '11 at 20:59
  • 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! – mark Nov 05 '11 at 03:58