简体   繁体   中英

PHP Zip File Empty?

I have a script that creates a zip files from files in a certain directory. After Download, for a lot of users - the zip file is empty. However, for other users - the file isn't empty. Is it something I'm doing wrong?

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

Better add some error control:

$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;

Additionally triple check your output buffering configuration vs. readfile .

I recommended this as a suggestion earlier, here is a skeleton of what I am talking about. It is untested as I don't have access to php at the moment.

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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