Search code examples
phpfirefoxfilenames

Firefox has problems when downloading with a space in filename


It seems that firefox has a problem with spaces within the filename for downloading...

header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
fputs($fp, $csvdata);
fclose($fp);

Here is an example of a file named: Test_ Grad Fair 2_20140129_1312_607.csv

When I attempt to download the file using the code above with FireFox the following occurs. (the main problem is it removes the file extension!)

Download Image from Firefox

And when I try downloading it from Safari, or Chrome:

Downloading it from Safari

   

I know a solution would be to maybe do something like:

$filename = str_replace(' ', '', $filename);

However, I prefer to figure out why FireFox is having this problem, it seems kinda ridiculous that you can't have a space in a filename. Could this be like a %20 instead of a space problem?


Solution

  • The filename parameter should be enclosed in double quotes.

    header( 'Content-Disposition: attachment;filename="'.$filename.'"');
    

    See http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download