<?
session_start();
include("../../../../../include/startup.php");
$query = "SELECT * FROM documents WHERE job_id = ".$_REQUEST['id']." ORDER BY document_date DESC ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$zip = new ZipArchive;
$zip_file_name = "download".rand().".zip";
if ($zip->open($zip_file_name,ZipArchive::CREATE) === TRUE) {	
	while($row = mysqli_fetch_array($sql)){
		$zip->addFile("files/".$row['document_name']);
	}
	$zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

ob_clean();
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$zip_file_name");

header("Pragma: no-cache"); 
header("Expires: 0"); 
header("Content-length: " . filesize($zip_file_name));
flush(); // Flush system output buffer
ob_end_clean();
readfile("$zip_file_name");
unlink($zip_file_name);
exit;	