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 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员