繁体   English   中英

PHP Zip 文件为空?

[英]PHP Zip File Empty?

我有一个脚本,可以从某个目录中的文件创建 zip 文件。 下载后,对于很多用户来说 - zip 文件是空的。 但是,对于其他用户 - 该文件不为空。 是我做错了什么吗?

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$id.'.zip"');
header('Cache-Control: private');
ob_clean();
flush();
readfile("".$id.".zip");
exit;

最好添加一些错误控制:

$file = "{$id}.zip";
if (!is_readable($file))
{
    die('Not accessible.');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: private');
ob_clean();
flush();
readfile($file);
exit;

另外三重检查您的 output 缓冲配置与readfile

我之前建议将此作为建议,这是我正在谈论的内容的骨架。 它未经测试,因为我目前无法访问 php。

$filename = $id . '.zip';
$handle = fopen($filename, 'r');
if ($handle === false)
{
    die('Could not read file "' . $filename . '"');
}

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: private');
while (!feof($handle))
{
    echo fread($handle, 8192);
}
fclose($handle);
exit;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM