4

Using Symfony 2.5 here, users upload MS Office files into our application and download it later as needed. Now, when the file attachment contains non-ascii chars (which is quite common as we are from Czech republic) Symfony raises error 'The filename fallback must only contain ASCII characters.'

I found many reports of this problem and discussions e.g.

https://github.com/symfony/symfony/issues/9093

... but no real solution. I know I can convert filename to ascci when making Content-Disposition header but it changes the filename presented to user which is not nice and quite misleading for user. Is there way how to avoid this and be able to serve former file name? Being able to download file with non-ascii chars in filename is quite common on internet so why this restriction?

By following this How to encode the filename parameter of Content-Disposition header in HTTP? I even tried to encode filename with urlencode() but now it says the % is not allowed char :-(

Update 1: Here is a code snippet I'm using. I'm using streaming the response to browser and headers are defined rather manualy I think.

$response = new StreamedResponse();
$response->headers->set('Content-Type', $upload->getMimeType());
$contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $upload->getFilename());
$response->headers->set('Content-Disposition', $contentDisposition);
Community
  • 1
  • 1
David Marko
  • 2,477
  • 3
  • 27
  • 58
  • 1
    On the github page the guy says, _“This is because of the way HTTP specifies the Content-Disposition header”_ – and while this is true for RFC 2616, this is superseded now by RFC 6266, which specifies an additional `filename*` parameter (note the `*` at the end) to use encodings other than ISO-8859-1. This will of course not help you with the original issue (but might help in arguing with the symfony guys on github if you feel like it). – CBroe Sep 05 '14 at 08:07
  • Until they are willing to change that, I suggest you look for a way to send that header yourself (by whatever means symfony provides for that), so that the header that BinaryFileResponse (or whatever you are using to create the download) creates gets overwritten with the one you specify. – CBroe Sep 05 '14 at 08:10
  • Thanks, I ipdated my question with the code snippet. How can I send a header myself in this case? – David Marko Sep 05 '14 at 08:37
  • And what method exactly raises the exception/error? If it is `makeDisposition` … then simply don’t use that method, but create the value yourself … – CBroe Sep 05 '14 at 08:57
  • I'm a bit stupid today :-( , sure it was caused by 'makeDisposition' method , I ommited it and create header on my own and its working now ... THANKS – David Marko Sep 05 '14 at 09:17

1 Answers1

5

Symfony 4 has $filenameFallback in HeaderUtils::makeDisposition.

Example

$filenameFallback = preg_replace(
    '#^.*\.#', 
    md5($filename) . '.', $filename
);

$disposition = $response->headers->makeDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT, 
    $filename, 
    $filenameFallback
);

$response->headers->set('Content-Disposition', $disposition);
localheinz
  • 9,179
  • 2
  • 33
  • 44
luchaninov
  • 6,792
  • 6
  • 60
  • 75
  • This should be validated answer. The $filenameFallback without non ASCII characters is for compliance with underlying protocol, and the $filename will in fact be the actual name of downloaded file for the user. So it solves the problem. – TomVerdier Sep 20 '21 at 01:46