I have some pdf files...I want to just download them as a collection and not one by one. For that , I am trying to zip all pdf files and download. But I don't know why when in my code I am changing ZipArchive file name, it is saying damaged and corrupt. My code is below:-
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
if($button=="Save as pdf")
{
$file_names = array();
foreach($a as $key=>$as)
{
$file_names[] = $key.'.pdf';
}
}
$archive_file_name='zipped.zip';
$file_path='/resume/';
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>
Can anybody help me out? Thanks in advance.
Its working fine but still i am facing 2 problems,1. I am getting error that Archive is corrupted if I change the name of archive_file_name to something other than given above. I don't why its happening 2.If I have got a zip file of say, 2 pdfs and then i again download a zip of only 1 pdf which was not same as earlier ones. But when I am downloading the zip of 1 pdf, then i am getting 3 pdfs......I don't know why ........please help me.