5

Consider that i am uploading a file with following names:

  1. test.pdf
  2. test file.pdf

When i download the test.pdf it's working fine..

But when i download test file.pdf it doesn't. The case is the space between the file name.

Unfortunately i should not change the file name when i upload (If i can i will set "_" using str_replace between spaces and resolve this issue).

Now how should i download the file ?

My code is given below:

header('Content-disposition: attachment; filename=test file.pdf');
header('Content-type: application/pdf');
readfile('test file.pdf');

Thanks in advance...

Fero
  • 12,969
  • 46
  • 116
  • 157
  • 1
    Try this link: http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http there are usefull comments. – XzKto Jul 22 '11 at 08:12

4 Answers4

5

The case is the space between the file name.

Put it in double quotes to fix the issue. With an example:

header('Content-disposition: attachment; filename="test file.pdf"');
header('Content-type: application/pdf');
readfile('test file.pdf');

This should work. I've just tried using readfile on a file with a space, and that seems to cause no issues.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
5

The content-disposition header is not part of HTTP (but popular to use with HTTP requests), see in RFC 2616.

The documentation of it is part of RFC 2183 which is in the MIME domain. It's specified as filename-param of which the filename is actually the value part. That value is defined in RFC 2045 Appendix A -- Collected Grammar as:

value := token / quoted-string
token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
             or tspecials>
tspecials :=  "(" / ")" / "<" / ">" / "@" /
              "," / ";" / ":" / "\" / <">
              "/" / "[" / "]" / "?" / "="
              ; Must be in quoted-string,
              ; to use within parameter values

See as well RFC 2616, 2.2 Basic Rules.

It's all US-ASCII and I assume the space needs escaping, e.g. multiple words in double-quotes (other chars work as well for that quoted string, token is a single word):

header('Content-disposition: attachment; filename="test file.pdf"');

Keep in mind that other chars need other escaping/encoding, see RFC 2047 and this pear ticket.

But in the end you need to test with every browser. I think it's easier if you replace the space with a _ or - inside the filename to be more stable:

header('Content-disposition: attachment; filename=test-file.pdf');

Hope this is helpful to understand your issue.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
2

If using the 'header' function for the downloading of files, especially if you're passing the filename as a variable, remember to surround the filename with double quotes, otherwise you'll have problems in Firefox as soon as there's a space in the filename.

So instead of typing:

<?php
  header("Content-Disposition: attachment; filename=" . basename($filename));
?>

you should type:

<?php
  header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
?>

If you don't do this then when the user clicks on the link for a file named "Example file with spaces.txt", then Firefox's Save As dialog box will give it the name "Example", and it will have no extension.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
2

Consider URL encoding the filename:

header('Content-disposition: attachment; filename=test%20file.pdf');
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • Some browsers *might* offer it with `%20` in the filename (as-is). – hakre Jul 22 '11 at 08:16
  • By encoding it, I collected some grammar in my answer: http://stackoverflow.com/questions/6786964/downloading-a-file-cause-error-when-it-contains-spaces-php/6787253#6787253 – hakre Jul 22 '11 at 08:32