4
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 to click on the link of the files, be downloaded as zip. I tried with the code below, but it did not work:
Page downloads.php
<?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();
?>
Ué, you generate a zip but serve the original archive... And I think you should close the zip before serving.
– bfavaretto
Hello bfavaretto. It gets to generate the zip but gives error while unzipping.
– user24136