繁体   English   中英

无法使用PHP将输出呈现到屏幕?

[英]Can't render output to screen using 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()。

http://php.net/ob_get_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中并将它们转储到标准输出。 如果您只想填充$ 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的输出并在需要时转储它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM