Ad

Downloads Start To Get Encased In Quotes

- 1 answer

I have recently experienced an issue with code that was working fine but then started to include quotes around the filename.

We have recently upgraded our server to PHP 7.2 and think this might be the cause of the change but was wanting to see if anyone might know for sure.

We are running Laravel 5.5 app on Ubuntu server with PHP 7.2 and has been observed to work and no longer work in Chrome (the main browser we use)

The following code was working fine for months:

$file = Storage::disk('s3')->get($location);

$headers = [
        'Content-Type' => 'xml',
        'Content-Description' => 'File Transfer',
        'Content-Disposition' => "attachment; filename='" . $realName . "'",
        'filename'=> $realName
    ];

return response($file, 200, $headers);

This would download example.xml fine

However it recently started to download 'example.xml' requiring me to remove the quotes to this:

$file = Storage::disk('s3')->get($location);

$headers = [
        'Content-Type' => 'xml',
        'Content-Description' => 'File Transfer',
        'Content-Disposition' => 'attachment; filename=' . $realName,
        'filename'=> $realName
    ];

return response($file, 200, $headers);

I have seen this problem with multiple file types and so is not file type related.

I am just seeking to discover the cause of this problem especially since the inclusion of the quotes is from what I understand is needed for backwards browser compatibility.

** Update **

I have found this related question but it does not answer the question that I am after:

PDF downloads surrounded by single quotes?

** Update 2 **

I have tested the signle quotes with multiple browsers and it does not work with Chrome and Firefox but does work with Internet Explorer & Edge at current versions.

Ad

Answer

Try this:

'Content-Disposition' => "attachment; filename=\"$realName\"",

It would seem that quoting the filename with single quotes is not valid: https://stackoverflow.com/a/31976390/6038111

Without the double quotes, my guess is that the browser is assuming the single quotes are part of the filename. I can't tell you what changed that caused it to stop working the way it was before, but I might guess that your browser updated and no longer supports the invalid single quoting.

Edit: This does indeed look like Chrome 72 changed its behavior: https://bugs.chromium.org/p/chromium/issues/detail?id=927913


Another Laravel-specific alternative is the built-in file download methods that are part of the storage system. This way you don't have to think about single vs. double quotes:

return Storage::disk('s3')->download($location, $realName, [
    'Content-Type' => 'text/xml',
    'Content-Description' => 'File Transfer',
]);

Note: This will first download the file to your server before sending it to the user (just like your code does), which adds latency due to the additional trip. Anyone coming here from Google might want to check out url() and temporaryUrl() available with S3, Rackspace, and any other filesystem drivers that support it: https://laravel.com/docs/5.7/filesystem#file-urls

Ad
source: stackoverflow.com
Ad