Download file as zip with PHP

1

I have the following file coming from the database:

$sqlAnexos = mysqli_query($this->conexao,"SELECT * FROM arquivos");

while($jmArquivos = mysqli_fetch_object($sqlAnexos)){

...
echo "<a href='download.php?key=".$jmArquivos->IdCodArquivos."'>".$jmArquivos->Arquivos."</a>";
...
}

I would like you to click on the file link to download it as a zip. I tried the code below, but it did not work:

Downloads.php page

<?php
 $sqlArquivos = mysqli_query($this->conexao,"SELECT * FROM arquivos WHERE IdCodArquivos = '".$_REQUEST["key"]."';");
 $jmArquivos = mysqli_fetch_object($sqlArquivos);


    $zipar = new ZipArchive();
    $arquivo = $jmArquivos->Arquivos;
    if($zipar->open('nome_arquivo_zip.zip', ZIPARCHIVE::CREATE) == TRUE){
       $zipar->addFile($arquivo,$arquivo);
    }else{
      echo "Erro";
    }
    header("Content-Type: application/zip");
    header("Content-Length: ".filesize($arquivo));
    header("Content-Disposition: attachment; filename=".basename($arquivo).".zip");
    readfile($arquivo.".zip");
    $zipar->close();
    ?>
    
asked by anonymous 23.05.2018 / 21:44

0 answers