使用PHP无法渲染输出到屏幕?
问题描述:
我有以下情况。使用PHP无法渲染输出到屏幕?
我有一个PHP网站女巫包含约3个动态内容的网页。我想将这个静态内容渲染到另一个文件。例如。 contact.php - > contact.html
我的代码看起来像这样
ob_start();
$content = require_once 'contact.php';
ob_end_flush();
file_put_contents("contact.html", $content);
不知怎的,这是不工作; - ?(
答
require_once()不返回由脚本输出的内容你需要得到存储在输出缓冲区中的脚本输出:
ob_start();
require_once('contact.php');
$content = ob_get_clean();
file_put_contents('contact.html', $content);
ob_get_clean():
获取当前缓冲区内容,并且 删除当前输出缓冲区。
ob_get_clean()实质上执行 ob_get_contents()和 ob_end_clean()。
答
ob_start();
require_once('contact.php');
$content = ob_get_contents();
ob_end_clean();
file_put_contents("contact.html", $content);
答
require_once
打开文件,并尝试分析它作为PHP。它不会返回它的输出。什么,你可能找的是:
<?php
ob_start();
require_once('file.php');
$content = ob_get_contents();
ob_end_flush();
// etc...
?>
这样一来,既脚本保存在$内容的数据,并将它们转储到标准输出。如果您只希望填充$ content,请使用ob_end_clean()
而不是ob_end_flush()
。
答
结帐ob_get_flush(http://www.php.net/manual/en/function.ob-get-flush.php)
基本上尝试做
if(!is_file("contact.html")){
ob_start();
require_once 'contact.php';
$content = ob_get_flush();
file_put_contents("contact.html", $content);
}else{
echo file_gut_contents("contact.html");
}
这应该从contact.php
缓冲输出并在需要时倾倒。
为什么不让浏览器试图加载'contact.php'? – pavium 2009-10-07 12:18:02