通过php下载的文件损坏/增加大小?

时间:2011-11-15 21:15:23

标签: php download zip

我有这段代码:

    $file = $tempDir . "/download.zip";
    // there's some omitted code here that creates the file that's to be downloaded

    if(file_exists($file) && is_readable($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/zip');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
    }else{
        return "Error: Failed to retrieve file.";
    }

生成文件的代码工作正常,在点击下载按钮后,我看到它出现在1 KB的适当位置。该文件也可用。当我下载时,它甚至说它是“973字节”。当文件实际下载时,它突然变为9.1 KB,并且完全损坏。为什么呢?

1 个答案:

答案 0 :(得分:0)

省略了代码运行后台程序吗?如果您运行命令行程序(exec等)来创建zip文件,您可能正在提供不完整的文件。

上传您知道可行的zip文件,删除代码以创建zip并查看是否正确提供。如果它不让我知道,我会看看。 这是windows还是linux?

文件的大小也可能略有不同。 Windows添加了可能影响文件大小的元数据,创建,添加,修改等