Skip to content

Instantly share code, notes, and snippets.

@ngocongcan
Created June 3, 2019 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngocongcan/f4c4d5f17e17c979c38e1e433eefd256 to your computer and use it in GitHub Desktop.
Save ngocongcan/f4c4d5f17e17c979c38e1e433eefd256 to your computer and use it in GitHub Desktop.
PHP-Symfony2-Note
public function massDownloadAction(Request $request) {
$documentids = $request->get('documentids', []);
$dir = $this->container->getParameter('documents_dir');
$repo = $this->getDoctrine()->getManager()->getRepository(Documents::class);
// Create new Zip Archive.
$zip = new \ZipArchive();
$now = date("Y-m-d H-i-s");
$zipName = 'Documents-' . $now . '.zip' ;
$zip->open($zipName, \ZipArchive::CREATE);
foreach ($documentids as $id) {
$doc = $repo->find($id);
if ($doc instanceof Documents) {
$filename = $doc->getAttachement();
$filePath = $dir . $filename;
if (file_exists($filePath)) {
$zip->addFromString(basename($filePath), file_get_contents($filePath));
}
}
}
$zip->close();
$response = new Response(file_get_contents($zipName));
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Disposition', 'attachment;filename="' . $zipName . '"');
$response->headers->set('Content-length', filesize($zipName));
@unlink($zipName);
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment