【问题标题】:Include a php file and write the output to a file? [duplicate]包含一个php文件并将输出写入文件? [复制]
【发布时间】:2014-09-09 13:54:48
【问题描述】:

而不是将 content.php 中的 PHP 代码输出到浏览器:

<?php

include 'content.php';

如何让执行结果不输出到浏览器而是写入本地文本文件:

<?php

include 'content.php';
// not output to browser
// but grab the results and write them to content.txt

如何做到这一点?

【问题讨论】:

  • 使文件返回一个变量并将该变量写入文件。
  • 我想你想要ob_get_contents

标签: php


【解决方案1】:

使用output buffering 捕获数据并使用file_put_contents() 保存捕获的输出。

ob_start();
include 'content.php';
$content = ob_get_clean();
file_put_contents('file.txt', $content);

【讨论】:

    【解决方案2】:

    您可以设置标题以下载文件,并使用file_get_contents 回显文件的内容。

    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="content.php"');
    $html = file_get_contents('content.php');
    echo $html;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 2012-10-29
      • 2012-11-01
      相关资源
      最近更新 更多