Error in Ziparchive::close() call

Asked

Viewed 129 times

0

When I’m using the method close to close the instance of ZipArchive I’m getting the following error:

Ziparchive::close(): Read error: Is a directory

The code of my application where the close is executed that’s:

$solicitacoes = Remessa::with('solicitacoes')
                        ->findOrFail($id)
                        ->solicitacoes()
                        ->with('usuario')
                        ->get();

$zip = new \ZipArchive();

$zipName = tempnam(sys_get_temp_dir(), 'remessa_');

if ($zip->open($zipName, ZipArchive::CREATE) === true) {

    $filesWithProblems = [];
    $filesNormal = [];

    $dir = public_path('imagens');

    $remessaId = zero_fill($id, 4);

    foreach ($solicitacoes as $solicitacao) {


        if ($solicitacao->usuario->nivel_id == 12) {

            $filename = $dir . "/colaboradores/{$solicitacao->usuario->id}/{$solicitacao->foto}";
            $filenameInZip = "$remessaId/$solicitacao->foto";

        } else {

            $filename = $dir . "/{$solicitacao->usuario->matricula}/{$solicitacao->foto}";
            $filenameInZip = "$remessaId/$solicitacao->foto";


        }

        if (file_exists($filename)) {
            $zip->addFile($filename, $filenameInZip);
            $filesNormal[] = $filename;
        } else {
            $filesWithProblems[] = $filename;
        }
    }
}


$zip->close();

I wanted to understand what this message means!

  • means that it is a directory, what content of the variable $zipName? The folder where you are writing the zip has the proper write permissions?

  • $zipName is a file in the temporary folder created by tempnam. Only in a specific shipment the problem occurs. In the rest it works normal.

  • I meant the same content, the string it’s generating, like, remessa1.zip, nome qualquer.zip. If it is only in a specific shipment maybe it is not some damaged/broken file causing it?

  • I found this in a comment in another post: The message: [PHP Error] ZipArchive::close(): Read error: Is a directory --> the answer is simple, it is just because there was some symbolic link in the file tree

  • You just want to add files right? Or folders too, just for a test try to change this if (file_exists($filename)) therefore if (is_file($filename))

  • I want to add files. Add one by one

Show 1 more comment
No answers

Browser other questions tagged

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