dongtu0363 2016-04-20 11:55
浏览 145
已采纳

Lighttpd X-Sendfile损坏了文件

Can anybody give me a hint on what is wrong? I have a script which generates a ZIP file on the fly and then serve it for download using X-Sendfile and lighttpd. Everything seems to be working but my downloaded file is a few bytes less compared to the zip file created on the server, and of course because it's the header of the file which is missing it results in broken zip file. Here is the relevant part of my script:

<?php
    header("Content-Disposition: attachment; filename=\"$MYVARIABLE1" . ".zip\"");

    if ($status == 1) {
        $tmp_file = tempnam('/tmp/', '');
        $dir = new RecursiveDirectoryIterator("/mnt/BLABLABLA/$MYVAR2/$MYVAR3/", FilesystemIterator::SKIP_DOTS);
        $it = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
        $it->setMaxDepth(1);
        $mylist = "";
        foreach ($result as $row) {
            $files = new RegexIterator($it, "/" . $row[2] . "/", RegexIterator::GET_MATCH);
            $mylist.="\"" . key(iterator_to_array($files)) . "\" ";
        }
        exec("/usr/bin/zip -qj0 $tmp_file $mylist");
        header("X-LIGHTTPD-send-file: $tmp_file" . ".zip");
        exit();
    }
?>

The zip on the server tmp folder is fine, but the downloaded content is corrupted.

  • 写回答

1条回答 默认 最新

  • duaabhuv188411 2016-04-21 09:42
    关注

    After tinkering around for a while I found the solution, yet not sure what it caused it. I needed to set the file size header before actually passing the file to X-Sendfile, so the code looks like this now:

        exec("/usr/bin/zip -qj0 $tmp_file $mylist");
        header("Content-Length: ".filesize($tmp_file.".zip"));
        header("X-LIGHTTPD-send-file: $tmp_file" . ".zip");
        exit();
    

    However this is not specified as a requirement in the Lighttpd Wiki, so it might be a bug.

    The problem was I had zlib.compression on and I was trying to send out a ZIP file which resulted in double compression. Turning off zlib.compression on runtime has fixed the issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行