Hi,
I have a client who wants their clients to be able to download their entire library of large pdf files from the website. So I've written code that is supposed to iterate through a query, add each file in the query to a temp zip file, and when the total number of files reaches 200, it's supposed to download the zip file and start a new zip file, downloading as many zip files as necessary to get the entire library.
The code does successfully get the first 200-file zip file, but it stops there. How do I get it to keep generating and downloading zip files?
Here's the relevant code:
Edit: (forgot) - this is the stream_zip_file function:
Thanks!
Katie
I have a client who wants their clients to be able to download their entire library of large pdf files from the website. So I've written code that is supposed to iterate through a query, add each file in the query to a temp zip file, and when the total number of files reaches 200, it's supposed to download the zip file and start a new zip file, downloading as many zip files as necessary to get the entire library.
The code does successfully get the first 200-file zip file, but it stops there. How do I get it to keep generating and downloading zip files?
Here's the relevant code:
PHP:
if ($rowcount>0) {
$maxnumfiles = 200;
$i = 0;
$zipnum = 1;
foreach($rows as $row) { //loop through the result array.
if ($i==0) {
$zipname = $rootdirprefix . 'downloads/' . sprintf('%04d', $clientid) . '_' . date('Y-m-d-His',time()) . '_' . $zipnum . '.zip';
$zip = new ZipArchive;
if ($zip->open($zipname, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$zipname>\n");
}
$debug_msg .= 'new zipfile created: ' . $zipname . '<br />';
}
$url = $rootdirprefix . 'docs/sds' . $row['URL'];
if (file_exists($url)) { //file exists... go ahead and add it.
$zip->addFile($url);
$i += 1;
$debug_msg .= 'file added: ' . $url . '<br />';
}
if ($i>=$maxnumfiles) {
$zip->close();
$debug_msg .= 'streaming zip file: ' . $zipname . '<br />';
stream_zip_file($zipname);
$i = 0;
$zipnum += 1;
}
}
if ($i>0) {
$zip->close();
$debug_msg .= 'streaming zip file: ' . $zipname . '<br />';
stream_zip_file($zipname);
}
}
Edit: (forgot) - this is the stream_zip_file function:
PHP:
function stream_zip_file($zipname) {
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
header("Pragma: no-cache");
header("Expires: 0");
readfile($zipname);
}
Thanks!
Katie