【问题标题】:PHP: put_file_content not working [closed]PHP:put_file_content 不工作 [关闭]
【发布时间】:2016-07-08 18:42:02
【问题描述】:

我有一个 php 脚本,可以将 eclipse birt 报告中的 pdf 保存为 pdf。 我正在使用获取文件内容作为输入。 birt 报告 pdf 需要一些时间来创建。

我认为这是问题所在。

以下是脚本:

<?php
$rname = 'reportname';
$wname = $rname . '_' . date('d.m.Y') . '.pdf';
$pdf = file_get_contents("http://xxx.xxx.xxx.x:8080/Birt/run?__report=" . $rname . ".rptdesign&sample=my+parameter&__format=pdf");
file_put_contents('/tmp/report' . $wname, $pdf);
?>

有什么问题?

感谢您的帮助:)

【问题讨论】:

  • 做了任何基本的调试,比如检查$pdf是否真的包含pdf数据?检查来自file_get_contents() 的返回值?你的任何代码都没有考虑到现实世界:事情失败了,你只是假设什么都不会。
  • 问题是,什么都没有改变......大约。两周后,pdf 正在运行。
  • PHP 中 HTTP 调用的默认超时时间为 60 秒。如果生成 pdf 的时间比这更长,那么 file_get_contents 最终将没有数据。如果它变得更快,那么它会突然开始成功。无论如何,请参阅@Daniel Lichtenberg 的回答,了解如何覆盖该超时。

标签: php pdf birt


【解决方案1】:

尝试为 file_get_contents 设置请求超时

file_get_contents('http://www.example.com/', false, stream_context_create(Array("http" => Array("method"  => "GET", 
    "timeout" => 600, 
    ))));

还要检查默认超时时间

echo ini_get("default_socket_timeout");

【讨论】:

    【解决方案2】:

    file_put_contents() 如果写入成功,将返回一个数值(表示写入的字节数),如果写入不成功,将返回false。如果写入不成功,则 Web 服务器可能没有写入目录的权限。确保目录是可写的。

    $result = file_put_contents('/tmp/report' . $wname, $pdf);
    
    if( is_numeric($result) && $result > 0 ) {
        // write was successful
    } else {
        // write was NOT successful
    }
    

    相关PHP文档:http://php.net/manual/en/function.file-put-contents.php

    【讨论】:

    • 权限是正确的,但我认为,从 php 获取文件内容有一个超时......报告的生成大约需要。 10 分钟
    猜你喜欢
    • 2013-02-04
    • 2014-05-04
    • 2016-04-26
    • 2012-10-08
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多