Error 500 when I generate zip with php - ziparchive

0

I'm trying to generate a zip file with php, I've downloaded a demo that works but when I deploy it on my server it returns error 500, what can it be?

Here I leave the code.

function agregar_zip($dir, $zip) {
  if (is_dir($dir)) {
    if ($da = opendir($dir)) {
      while (($archivo = readdir($da)) !== false) {

        if (is_dir($dir . $archivo) && $archivo != "." && $archivo != "..") {
          echo "<strong>Creando directorio: $dir$archivo</strong><br/>";
          agregar_zip($dir . $archivo . "/", $zip);

        } elseif (is_file($dir . $archivo) && $archivo != "." && $archivo != "..") {
          echo "Agregando archivo: $dir$archivo <br/>";
          $zip->addFile($dir . $archivo, $dir . $archivo);
        }
      }
      closedir($da);
    }
  }
}

$zip = new ZipArchive();
$dir = 'fuente/'; //Dir a comprimir
$rutaFinal = "archivos/"; //Dir donde se almacenará

if(!file_exists($rutaFinal)){
  mkdir($rutaFinal);
}

$archivoZip = "pruebazip.zip"; //nombre del fichero.zip

if ($zip->open($archivoZip, ZIPARCHIVE::CREATE) === true) {
  agregar_zip($dir, $zip);
  $zip->close();

  rename($archivoZip, "$rutaFinal/$archivoZip");

  if (file_exists($rutaFinal. "/" . $archivoZip)) {
    echo "Proceso Finalizado!! <br/><br/>
                Descargar: <a href='$rutaFinal/$archivoZip'>$archivoZip</a>";
  } else {
    echo "Error, archivo zip no ha sido creado!!";
  }
}
    
asked by Fabián Moreno 29.05.2018 в 18:29
source

0 answers