【问题标题】:Php ob_get_contents failphp ob_get_contents 失败
【发布时间】:2012-12-11 10:07:17
【问题描述】:

我正在使用 ob_get_contents() 从 php 文件创建一个 html 文件。在开发机器上有时它可以工作,但在测试机器上它不能工作。

<html>
<body>
    <div>
       //some html content
    </div>
</body>
</html>

<?php
    ob_start();
    file_put_contents('./pdfreportresult.html', ob_get_contents());

    require_once (APP_DIR . 'assessment/wkhtmltopdf/snappy-master/src/autoload.php');

    use Knp\Snappy\Pdf;

    $snappy = new Pdf(APP_DIR . 'assessment/wkhtmltopdf/wkhtmltopdf.exe');

    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="report.pdf"');
    echo $snappy->getOutput(APP_DIR . 'assessment/pdfreportresult.html');
    ob_end_clean();
?>

我检查了测试机器的 php.ini 中的 output_buffering,它就像在开发机器上一样“打开”。当我检查我创建的 html 文件“pdfreportresult.html”时,它为空或存在一半内容。

也许问题可能与缓冲区大小有关,我尝试使用 ob_clean() 而不是 ob_end_clean() 仍然无法正常工作。

【问题讨论】:

    标签: php output-buffering ob-get-contents


    【解决方案1】:

    在您开始输出内容之前启动缓冲区。另外,完成后清理缓冲区。

    <?php
    ob_start();
    ?>
    <html>
    <body>
        <div>
           //some html content
        </div>
    </body>
    </html>
    
    <?php
    file_put_contents('./pdfreportresult.html', ob_get_contents());
    ob_end_clean();
    ...
    

    【讨论】:

    • 非常感谢您在开发和测试机器上工作。顺便问一下,缓冲会不会给系统上的多个用户带来问题?
    • @cgrgcn 缓冲可能会降低多个用户之间并发问题的可能性。
    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2015-04-01
    • 2019-06-22
    • 2017-04-04
    • 2016-11-13
    • 2012-12-29
    • 2018-04-25
    相关资源
    最近更新 更多