Reputation: 23
I have a local DB table in which many PDF links are saved around 15000. I want to download all that PDF on one click but my problem is its opening PDF not downloading. I was trying this method.
items = Array.from(document.getElementsByTagName("a"));
items.forEach(function(item) {
link = item.href;
if (link.substr(link.length - 4) == ".pdf") {
filename = link.replace(/^.*[\\\/]/, '');
item.download = filename;
item.click();
}
});
Upvotes: 0
Views: 78
Reputation: 23
Thanks for your reply, this code works for me with zip archive
$files = array('pdflink','pdflink');
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
foreach($files as $file){
$download_file = file_get_contents($file);
$zip->addFromString(basename($file),$download_file);
}
$zip->close();
header('Content-disposition: attachment; filename=file.zip');
header('Content-type: application/zip');
readfile($tmp_file);
?>
Upvotes: 0
Reputation: 256
You can not download all files using only 1 click. Instead of You can use ZIP Archive Class in PHP.
Make one zip file of all available pdf and download it.
$files = array('pdf1.pdf','pdf2.pdf');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
And Headers Like
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
Upvotes: 4