My issue is that I am compressing more than two files in format .ZIP
from a server. When doing local tests on Windows it works without problems, when running it on Linux or Mac systems locally or even on a server, an error occurs when decompressing the file.
What do I have to take into consideration to do this kind of thing, i.e. encodings of the files to be compressed, use the command exec
instead of the ZipArchive
PHP class ?
EDITION:
I already managed to fix the error. The problem was adding the files to the .ZIP... I'll 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);
}
}
The ZipArchive extension must exist, not all distros have it by default:
On Linux use (depending on the distro):
to install it and if it doesn't work look for the package with:
Don't forget to restart your web server, be it Nginx or Apache.
Check for the existence of this extension using the function
phpinfo();
on any page or runphp -i|grep more
from the command line.