public setContentDisposition ( string $disposition, string $filename = '', string $filenameFallback = '' ) : |
||
$disposition | string | ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT |
$filename | string | Optionally use this filename instead of the real name of the file |
$filenameFallback | string | A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename |
return |
public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
{
if ($filename === '') {
$filename = $this->file->getFilename();
}
if ('' === $filenameFallback && (!preg_match('/^[\\x20-\\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
$encoding = mb_detect_encoding($filename, null, true);
for ($i = 0; $i < mb_strlen($filename, $encoding); ++$i) {
$char = mb_substr($filename, $i, 1, $encoding);
if ('%' === $char || ord($char) < 32 || ord($char) > 126) {
$filenameFallback .= '_';
} else {
$filenameFallback .= $char;
}
}
}
$dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
$this->headers->set('Content-Disposition', $dispositionHeader);
return $this;
}
public function invoiceAction(Convention $convention, Request $request) { $registrations = $convention->getRegistrations(); $zip = new ZipArchive(); $title = $convention->getSlug(); $filename = tempnam('/tmp/', 'ritsiGA-' . $title . '-'); unlink($filename); if ($zip->open($filename, ZIPARCHIVE::CREATE) !== true) { throw new \Exception("cannot open <{$filename}>\n"); } if (false === $zip->addEmptyDir($title)) { throw new \Exception("cannot add empty dir {$title}\n"); } $route_dir = $this->container->getParameter('kernel.root_dir'); foreach ($registrations as $registration) { $single_file = $route_dir . '/../private/documents/invoices/' . $registration->getId() . '.pdf'; if (false === file_exists($single_file)) { continue; } $name = $registration->getId(); if (false === $zip->addFile($single_file, implode('/', array($title, $name . '.pdf')))) { throw new \Exception("cannot add file\n"); } } if (false === $zip->close()) { throw new \Exception("cannot close <{$filename}>\n"); } $response = new BinaryFileResponse($filename); $response->trustXSendfileTypeHeader(); $response->prepare($request); $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $convention->getSlug() . '-facturas.zip', iconv('UTF-8', 'ASCII//TRANSLIT', $convention->getSlug() . '-facturas.zip')); return $response; }