I need to download a zip file from a website as I am going to require to coalesce multiple files in a single download (up to 100 individual files-ish).
When attempting to create the zip file, it downloads as intended, the file name appears in the format "YYYY.MM.DD - HH.MM.SS" also as intended. My issue occurs when attempting to open the zip file in windows 7 (or winzip) - I get the error message below. This happens repeatedly from multiple attempts.
I assume that I have made an error while coding the creation or download of the zip file, rather than the zip file format itself being an issue as I can open different zip files - can anyone see the mistake I have probably made? (code included below error image)
I have attempted to use Download multiple files as a zip-file using php as a reference.
//backup file name based on current date and time
$filename = date("Y.m.j - H.i.s");
//name of zip file used when downloading
$zipname = 'temp.zip';
//create new zip file
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
//yes, I know the zip file is empty currently - I've cut the code from here for
//now as the zip file doesn't function with / without it currently
$zip->close();
//download file from temporary file on server as '$filename.zip'
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$filename.'.zip');
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
a)
Some PHP error occurs and is printed to stdout, so it is included in the ZIP file (which, of course, becomes corrupted, as there shouldn't be random characters from PHP errors inside it :D).b)
EIther PHP or the webserver adds another layer of GZIP ontop of it, because of mod_deflate or similar. The headers don't respect this however, therefore the ZIP file is not "un-gzip'd" by the browser, and therefore corrupted.ob_clean();
immediately before starting to output the new headers.ob_clean()
, the final zip file becomes about 10kb bigger and it works agian. I have no idea why. But I can forget about it now.