Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 883
Arian Faurtosh Avatar asked Jan 29 '14 21:01

Arian Faurtosh


People also ask

How do I change the download format in Firefox?

In the General panel, go to the Applications section. Select the type of file you wish to change and use the drop-down menu to change the download action, such as using another application to open that type of file or having Firefox always ask you what to do.

Does Firefox have a download size limit?

Firefox Can't Download Files Larger Than 1GB.


2 Answers

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

like image 100
Barmar Avatar answered Oct 05 '22 02:10

Barmar


I had a small issue while downloading a file with file name contains space. I fixed this issue using the following code. I had written this code for CodeIgniter frame work.

    $file_name = 'Test Pdf.pdf'; //Your file name
    $original_file_name = 'Test Pdf.pdf'; //Your file name
    $file_name = str_replace(' ', '%20', $file_name);
    $file_url = asset_url("uploads/agent_logo/46/".$file_name);// Full file path including file name.
    ob_start();
    ob_end_clean();
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: Binary');
    header("Content-Disposition: attachment; filename=\"" . $original_file_name . "\"");
    readfile($file_url);

This will work for all type of files and all browsers including mozilla firefox and IE

like image 29
Abin John Avatar answered Oct 05 '22 01:10

Abin John