Cannot Destroy the zip context

Asked

Viewed 93 times

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?

  • @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 function tempnam automatically.

  • 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.

  • @Marcelodeandrade but I don’t actually create the directory. The function sys_get_temp_dir returns the name of the temporary folder of the Operating System. Actually, it is using the /tmp on Linux, and this folder exists. I will try to give permission in this folder to see if it solves the problem :D

  • You’re calling $zip->close() within the if ($zip->open(..), take the return from there too. That must be the cause of the error. :)

  • @Zekk son, I need the return. Otherwise, Laravel doesn’t download.

  • @Wallacemaxters I expressed myself badly, put the return outside the context of if.. something like this: http://pastebin.com/GceS8r4V

  • @zekk has a Try/catch encompassing everything. I took to put in question :D

  • @Wallacemaxters Cool :) So use try to create the zip, catch to deal with exceptions and the finally to close the instance of zip. Take a look in that question also. I hope it helps.

  • @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)

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.