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);
}
}