0
What causes this mistake?
I have a code where I create a Zip through the class ZipArchive.
I read a certain directory and take all the image files present in it. I add it in the ZIP and, at the end, the user downloads this zip. This Zip is created in the default OS temporary folder.
This is my code:
function getDownloadFotosZip($remessaID)
{
$solicitacoes = Remessa::findOrFail($remessaID)->solicitacoes;
$zip = new \ZipArchive();
$zipName = tempnam(sys_get_temp_dir(), 'remessa_');
$remessaIDComZeros = zero_fill($remessaID, 4);
if ($zip->open($zipName, ZipArchive::CREATE) === true) {
$filesWithProblems = [];
foreach ($solicitacoes as $solicitacao) {
$filename = $solicitacao->foto_fullpath;
$filenameInZip = "{$remessaIDComZeros}/{$solicitacao->codigo}.jpg";
if ($filename && File::exists($filename)) {
$zip->addFile($filename, $filenameInZip);
} else {
$filesWithProblems[] = $filename;
}
}
if (($count = count($filesWithProblems)) > 0) {
$errorMessage = "{$count} foto não foram encontrados no sistema:\n" . implode(PHP_EOL, $filesWithProblems);
$zip->addFromString('erros.txt', $errorMessage);
}
$zip->close();
return Response::download($zipName, "remessa_{$remessaIDComZeros}.zip");
}
}
The following error is being generated
Exception 'Errorexception' with message 'Producaocontroller::getDownloadFotosRemessa(): Cannot Destroy the zip context' in /var/www/newtonpaiva/app/controllers/Producaocontroller.php:0
Is making a mistake Cannot destroy the zip context on the famous line 0.
Observing: I am using the Laravel framework, but I will not add tags, because the problem is specific to ZipArchive, and not the Framework.
What is the solution to this?
Checked if the directory that is catching the zipname is with sufficient permission and the
remessa_exists?– Marcelo de Andrade
@Marcelodeandrade in PHP o
remessa_that I used is for it to generate a temporary file name with a prefix. The file is created by the functiontempnamautomatically.– Wallace Maxters
Try to leave already created the directory without using the function, I had a similar problem and the problem was not the location of it.
– Marcelo de Andrade
@Marcelodeandrade but I don’t actually create the directory. The function
sys_get_temp_dirreturns the name of the temporary folder of the Operating System. Actually, it is using the/tmpon Linux, and this folder exists. I will try to give permission in this folder to see if it solves the problem :D– Wallace Maxters
You’re calling
$zip->close()within theif ($zip->open(..), take thereturnfrom there too. That must be the cause of the error. :)– stderr
@Zekk son, I need the
return. Otherwise, Laravel doesn’t download.– Wallace Maxters
@Wallacemaxters I expressed myself badly, put the
returnoutside the context ofif.. something like this: http://pastebin.com/GceS8r4V– stderr
@zekk has a Try/catch encompassing everything. I took to put in question :D
– Wallace Maxters
@Wallacemaxters Cool :) So use
tryto create the zip,catchto deal with exceptions and thefinallyto close the instance of zip. Take a look in that question also. I hope it helps.– stderr
@zekk the point is that the code was working perfectly before. I didn’t have to mess with the logic, the only thing I did was pull the server (I might have to see if it’s permission)
– Wallace Maxters