I´m trying to make a zip with some file in my DB, and I do just some other things before I packed on a zip, I actually read files to another server and write them in the same folder to packing, but for some reason my zip file downloaded as invalid.
I actually see the answers related to this issue but they did not help me. Here´s my script:
<?php
include_once('..databaseconnection');
include_once('..databaseconnection');
function noticiaZip($idNoticia)
{
//Utilizo el idTarea para traer las tareas del post correspondiente
sql="Query->";
$result = mysql_query($sql);
$arrgArch = array();
while ($row = mysql_fetch_array($result)) {
$arrgArch[] = array('Archivo' =>$row['link'],
'Nombre'=>utf8_encode($row['Nombre']),
'Titulo'=>utf8_encode($row['Titulo'])
);
}
$zip = new ZipArchive();
$filename = 'example.zip';
if($zip->open($filename, ZipArchive::CREATE)===true) {
foreach ($arrgArch as $value) {
//Divido el url para poder rescatar el nombre del archivo
$nameFile = explode('/', $value['Archivo']);
$count = count($nameFile);
$name = $count - 1;
//Url con el code para los espacios y que me acepte realizar el stream
$url = str_replace($nameFile[$name], rawurlencode($nameFile[$name]), $value['Archivo']);
$source =$url;
$destination = $nameFile[$name];
$data = file_get_contents($source);
$handle = fopen($destination, "w+");
$noticias = fopen("noticia.doc", "w+");
fwrite($handle, $data);
fclose($handle);
$verNoticia=traeNoticiaN($idNoticia);
foreach($verNoticia as $k => $v){
fwrite($noticias, "Título:
".$v['Titulo']."
");
fwrite($noticias, "Título:
".$v['value']."
");
fwrite($noticias, "Título:
".$v['value']."
");
fwrite($noticias, "Título:
".$v['value']."
");
fwrite($noticias, "Título:
".$v['value']."
");
fwrite($noticias, "Título:
".$v['value']."
");
fwrite($noticias, "Título:
".$v['value']."
");
fclose($noticias);
}
if($value!='noticiaZip.php') {
$zip->addFile("temp/".$destination, $destination);
$zip->addFile("temp/noticia.doc", "noticia.doc");
}
}
$zip->close();
foreach ($arrgArch as $value) {
$nameFile = explode('/', $value['Archivo']);
$count = count($nameFile);
$name = $count - 1;
$destination = $nameFile[$name];
unlink($destination);
unlink("noticia.doc");
}
} // Set headers
header("Content-type: application/zip");
header("Content-disposition: attachment; filename=".basename($filename));
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public"); // Sin esto el IE no funciona
header("Content-Transfer-Encoding: binary");
header("Content-Length:".filesize($filename));
ob_clean();
readfile($filename);
unlink($filename);
exit();
}
$noti = noticiaZip($_GET['idNoticia']);
?>