Skip to content

Instantly share code, notes, and snippets.

@Xatenev
Created August 19, 2016 14:43
Show Gist options
  • Save Xatenev/e96c34701633ce497bbdcb052163502f to your computer and use it in GitHub Desktop.
Save Xatenev/e96c34701633ce497bbdcb052163502f to your computer and use it in GitHub Desktop.
file.php
function outputFileDownload($files, $articleFiles) {
$filePrefix = 'Downloads_-_';
$zipname = "blc_files.zip";
$zip = new ZipArchive();
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $fileName => $fileValue) {
$allowedDownload = false;
foreach ($articleFiles as $articleFile) { // Security check here because otherwise people could download all files from the whole wiki: only allow file downloads for files attached to downloads page
if ($filePrefix . $fileName == $articleFile->img_name) {
$allowedDownload = true;
}
}
if ($allowedDownload) {
$wfFile = wfFindFile($filePrefix . $fileName);
$zip->addFile($wfFile->getLocalRefPath(), $fileName);
}
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
return unlink($zipname);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment