Problem with the ZipArchive class in php

0

My question is that I am compressing more than two files in .ZIP format from a server. When doing local tests in Windows it works without problems, when running it on Linux or Mac systems locally or even on a server, an error occurs at the time of unzipping the file.

What should I take into consideration to do this kind of thing, that is, encodings of the files to be compressed, use the exec command instead of the ZipArchive class of PHP ?

EDITION:

I have already managed to fix the error. The problem was the addition of the files to the .ZIP .... I will leave the code that works for me:

$folder = "ubicacion-archivos/";        
    if (extension_loaded('zip')&&$result) {
            //Se crea la clase Zip
            $zip = new ZipArchive();
            $nombreZip = "file.zip";
            //Se crea el archivo Zip
            if ($zip->open($nombreZip,ZIPARCHIVE::CREATE)!==TRUE)
                $error .= "Error con el zip";

            while ($objetoZip = mysqli_fetch_object($result))
                    $zip->addFile($folder.$objetoZip->archivo,$objetoZip->archivo);

            //Se agrega el archivo excel al Zip
            $zip->addFile('archivos-a-agregar');    
            //Se cierra el archivo Zip
            $zip->close();

            //Si el archivo zip se creó con exito
            if (file_exists($nombreZip)) {
                    header("Content-Type: application/zip");
                    header('Content-Disposition: attachment; filename="'.$nombreZip.'"');
                    header("Pragma: no-cache");
                    header("Expires: 0");
                    readfile($nombreZip);
                    //Se elimina el Zip del servidor
                    unlink($nombreZip);
            }
    }
    
asked by Iras 07.12.2016 в 19:20
source

1 answer

1

The ZipArchive extension must exist, not all the distros bring it by default:

In Linux use (depending on the distro):

sudo apt-get install php-zip
sudo yum install php-zip

to install it and if it does not work look for the package with:

sudo apt-cache search php-zip
sudo yum search php-zip

Do not forget to restart your Web server, either Nginx or Apache.

Check for the existence of this extension by using the phpinfo(); function on any page or by running php -i|grep more from the command line.

    
answered by 08.12.2016 / 23:22
source