0

I'm uploading a file to a server then I store the original file name in a database.

Now for downloading I am currently using something like this:

Content-Disposition: attachment; filename=<?=urlencode($file)?>

It works but it replaces space characters with +

How can I restore the original filename when downloading?

IMB
  • 15,163
  • 19
  • 82
  • 140
  • the `urlencode` function is encoding your string - dont encode it !?! – Manse Apr 24 '12 at 14:39
  • @ManseUK If I don't it breaks when there's a space character. – IMB Apr 24 '12 at 14:40
  • Possible duplicate: http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http – jeremyharris Apr 24 '12 at 14:42
  • @Mr.Disappointment There's no need to `urldencode` on receipt, the only reason I use `urlencode` is because it breaks when there's spaces. Now I realize I just need to put quotes so I don't need to use `urlencode` or `urldecode` anymore. – IMB Apr 24 '12 at 14:48
  • @Mr.Disappointment Yup that's how it's implemented, I only encountered the problem when I started downloading using user-friendly name which in my case was the original filename. – IMB Apr 24 '12 at 14:56

1 Answers1

2

The urlencode function is putting the + signs in your filename string - if you dont want the + signs remove the urlencode - then put the string in quotes :

header('Content-Disposition: attachment; filename="'.$file.'"');
Manse
  • 37,765
  • 10
  • 83
  • 108