What causes this error?
I have a code where I create a Zip
through class ZipArchive
.
I read a certain directory and get all the image files present in it. I add it to the ZIP file and, at the end, the user downloads it. This Zip is created in the default OS temporary folder.
My code is this:
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 (): Can not destroy the context context' in /var/www/newtonpaiva/app/controllers/ProducaoController.php:0
You are giving% error of% in the famous line Cannot destroy the zip context
.
Note : I'm using the Laravel framework, but I will not add tags, because the problem is specific to 0
, not Framework.
What is the solution to this?