Closed rudischenck closed 1 year ago
The exit
method expects an int for the console. https://www.php.net/manual/en/function.exit.php
So this will not work:
// send zip to client
exit($zipString);
You may try the readfile
function instead:
$zip = new ZipFile();
$zip->addDirRecursive($directory);
$zip->saveAsFile($zipFileName);
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=\"$trimmed_name_dir.zip\"");
// send zip to client
readfile($zipFileName);
exit;
The
exit
method expects an int for the console. https://www.php.net/manual/en/function.exit.phpSo this will not work:
// send zip to client exit($zipString);
You may try the
readfile
function instead:$zip = new ZipFile(); $zip->addDirRecursive($directory); $zip->saveAsFile($zipFileName); $zip->close(); header('Content-Type: application/zip'); header("Content-Disposition: attachment; filename=\"$trimmed_name_dir.zip\""); // send zip to client readfile($zipFileName); exit;
Unfortunately, this did not work either. I did find a specific instance where I removed an animated .gif file from the directory targeted for compression, the library was able to open the .zip file using ZipFile::openFile()
.
In my previous post I omitted the code that gets and saves the images to the target directory. The code is below.
foreach ($this->localizations as $localization) {
preg_match_all('/<img[^>]+>/i', $localization->get('body'), $imgTags);
foreach ($imgTags[0] as $img) {
preg_match('/src="([^"]+)/i', $img, $imgSrc);
$imgSrc = $imgSrc[1];
if ($imgSrc && !in_array($imgSrc, $imageLinks)) {
$imgData = file_get_contents($imgSrc);
if (!$imgData) {
continue;
}
$imageLinks[] = $imgSrc;
$imgName = basename($imgSrc);
file_put_contents($directory . $imgName, $imgData);
}
}
}
Found the issue. The uploads were chunked and I wasn't reading all of them before trying to unzip the file.
Description
Using the code snippet below to export a directory
Sometimes, when I call
ZipFile::openFile()
on a file exported in this manner, the exceptionInvalid zip file. The end of the central directory could not be found.
is thrown.I've exported an object that fails without including the images I need and it works as expected. When the images are included in the directory it does not work. Bash also cannot open the .zip file, the output is in the following screenshot -
The .zip file can be opened and its contents extracted normally in Windows 10. Outside of corrupted images (maybe?) I have no idea what might be causing it.
I've also attached the .zip file in question.
How to reproduce
The code below is somewhat simplified, I'm exporting objects/models, but these snippets should give the jist.
Download a zip
reupload and open the zip
Possible Solution
I've tried using
$zip->outputAsString()
, didn't work. I've tried saving to a file using$zip->saveAsStream()
, didn't work ($zip->saveAsStream()
seems counter-productive given it closes the stream). I've tried using$zip->outputAsAttachment()
, didn't work.No idea. I'm lost. I've been working on it for the past 5 hours and haven't gotten anywhere.
Additional context
Zip file attached.
What_You_Can_Do_to_Help_Secure_the_Authentication_System.zip