【问题标题】:PHP includes not working within file_get_contentsPHP 包括不在 file_get_contents 中工作
【发布时间】:2014-09-08 09:57:43
【问题描述】:

我有以下代码将变量设置为文件的内容,然后在模板文件中使用该变量。

$content = file_get_contents('./section_body.php');

include $_SERVER['DOCUMENT_ROOT'] . '/includes/template.php';

在我将包含添加到 section_body.php 之前,这一切正常。此包含的内容不会呈现到页面。

任何想法我哪里出错了?

如果有帮助,这里是 section_body.php 的代码:

<?php 

include $_SERVER['DOCUMENT_ROOT'] . '/controllers/category_list.php';

?>

<div>
  <h1>Title</h1>
  <p>dfg kjdfsghsd fkjghdjksfg jdhksfg kjsdfg hjskdf ghjsdfg kdhjsfg </p>
</div>

【问题讨论】:

  • file_get_contents 不执行 php 代码,它只是以文本或二进制形式读取文件。
  • 有道理!你能给我指出解决问题的正确方法吗?我认为输出缓冲是最好的方法是对的吗?
  • 我想这个答案是给你的,link

标签: php include file-get-contents


【解决方案1】:

你可以在没有 file_get_contents() 的情况下得到如下渲染的内容,这样你就可以减少处理时间。

 ob_start();
 include('./section_body.php');
 $content=ob_get_clean();

【讨论】:

    【解决方案2】:

    您似乎正在尝试为您的网站构建模板结构。现有的很多,这也是一种选择。

    您可以使用 PHP output control functions,从 [ob_start()] 开始。(http://php.net/manual/en/function.ob-start.php)

    <?php
        ob_start();
        include $_SERVER['DOCUMENT_ROOT'] . "section_body.php";
        $content = ob_get_contents();
        ob_end_clean();
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/template.php';
    ?>
    

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 2014-07-20
      • 1970-01-01
      • 2011-08-11
      • 2014-08-15
      • 1970-01-01
      • 2011-12-01
      • 2018-09-13
      • 1970-01-01
      相关资源
      最近更新 更多