简体   繁体   中英

php zipfile returning an empty zip file

<?php

$files = array('1.txt', '2.txt', '3.txt');
$zipname = 'test.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

?>

this is my code, the only issue is it is not downloading the files i have specified.

am i doing something wrong? is it the browser im using? (opera)

the file downloads but it ends up being empty.

First check zip file is creating or not and then,

Add File transfer header...

        header('Content-Description: File Transfer');
        header('Content-Type: application/zip');
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: Keep-Alive');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' ."Your file size" );            
        set_time_limit(0);

It will work.

Append "Your file size" to the length of the file size.

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