Created
June 3, 2019 08:54
-
-
Save ngocongcan/f4c4d5f17e17c979c38e1e433eefd256 to your computer and use it in GitHub Desktop.
PHP-Symfony2-Note
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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