开发者

PHP Output buffering on the command line

开发者 https://www.devze.com 2023-02-22 01:21 出处:网络
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.

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?


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号