我有这样的代码:
$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.";
}生成文件的代码运行良好,点击下载按钮后,我看到它出现在适当的位置,大小为1KB。该文件也是可用的。当我下载的时候,它甚至说它是"973字节“。然而,当文件真正下载时,它突然变成了9.1KB,并且完全损坏了。为什么?
发布于 2011-11-16 05:55:51
被省略的代码是否运行后台程序?如果您运行命令行程序(exec等)来创建zip文件,那么您提供的可能是一个不完整的文件。
上传一个你知道可以工作的zip文件,删除创建zip的代码,看看它是否被正确提供。如果不是,请告诉我,我会看一看。这是windows还是linux?
此外,文件的大小可能会略有不同。windows添加元数据、创建、添加、修改等会影响文件大小
https://stackoverflow.com/questions/8143234
复制相似问题