PHP命令行上的输出缓冲 [英] PHP Output buffering on the command line
问题描述
我有一个PHP脚本,我想在命令行上运行。此脚本需要加载包含PHP和HTML内容的PHP文件,并从该文件获取渲染的输出。
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();
}
?>
在浏览器中运行时,我的脚本通过include()获取PHP文件的输出,并将输出存储在$ content中。
When run in the browser, my script gets the rendered output of the PHP file via include(), and stores the output in $content.
但是,当我在命令行上执行这个脚本时,PHP文件的内容被回显,$ 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.
我搜索了文档,但似乎没有什么工作。调用ini_set('implicit_flush',false)没有效果,也没有ob_implicit_flush(0);
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);
任何想法?
推荐答案
我无法使用PHP 5.3重复此问题。
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 ~]$
在交互式提示符下:
[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;
这篇关于PHP命令行上的输出缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!