ZipArchive PHP-尽管到目前为止在stackoverflow上关注每一个答案,但仍不断出现错误


ZipArchive PHP - constantly getting errors despite following every answer on stackoverflow so far

我用MySQL查询创建了一个文件。现在尝试将其作为zip下载。我想加入第二个文件,但我甚至找不到一个来工作。

WinRar只是失败了。在另一个windows系统上,我收到一个错误,说归档是空的,尽管file_exists是空的。

$file = '/temp/' . $temp_filename;
if(file_exists($file)) {
            $zipname = 'myfile_' . $name . '_' . $date . '.zip';
            $zip = new ZipArchive;
            $zip->open($zipname, ZipArchive::CREATE);
            $download_file = file_get_contents($file);
            $zip->addFromString(basename($file),$download_file);
            $zip->close();
            // also tried $zip->addFile($file,basename($file));

            //header('Content-Description: File Transfer');
            header('Content-Type: application/zip');
            header('Content-Disposition: attachment; filename='.basename($zipname));
            //header('Content-Transfer-Encoding: binary');
            //header('Expires: 0');
            //header('Cache-Control: must-revalidate');
            //header('Pragma: public');
            header('Content-Length: ' . filesize($zipname));
            header('Pragma: no-cache');
            header('Expires: 0');
            ob_clean(); // also tried commenting this
            flush(); // and this
            readfile($zipname);
            // exit(); // tried with and without exit.
}

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Disposition:attachment; filename=test.zip
Content-Type:application/zip
Date:Sat, 28 Feb 2015 01:04:57 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.7 (Ubuntu)
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.9-1+sury.org~precise+1

ZipArchive::如果文件存在,CREATE将失败,请尝试:

$zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);