0

I have a php script that opens a download dialog to a file that is on the server. It works for the most part but if a file has an '&' in the filename, then when you try to open it on the client's computer it is corrupt. I tried different file types and xls, xlsx, and pdf have all been corrupt (that's all I've tried). Any ideas on how to fix this? The code is below.

header("Cache-control: private");
header("Content-Type: " . $oFile->getMimeType()); 
header("Content-Length: " . $oFile->getFileSize());
header("Content-Disposition: attachment; filename=" . $fileName);
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Pragma: public");

EDIT: When I open the file in a text editor, either Notepad++ or Sublime Text, it shows no content in the file.

bmbaeb
  • 520
  • 1
  • 8
  • 15
  • 1
    Corrupt in what way? Have you tried looking inside? – zerkms May 16 '12 at 21:46
  • 1
    Related: http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http – ChristopheD May 16 '12 at 21:48
  • For a name with spaces in it (or a few other special characters), you must put the file name in quotes. `header("Content-Disposition: attachment; filename=\"{$fileName\"");` Edit: I take that back. Apparently quotes are not standard. – Corbin May 16 '12 at 21:48
  • @zerkms By corrupt I mean the program won't open them and gives an error that the file is corrupt. – bmbaeb May 16 '12 at 21:50
  • @bmbaeb: I understand that. And my questions are still actual – zerkms May 16 '12 at 21:50
  • Try this: rename the file to something clean, then open it. Report what you see. – alexis May 16 '12 at 21:51
  • @alexis: why instead of **guessing** not open broken file and see **for sure** what's wrong with it? – zerkms May 16 '12 at 21:52
  • I tried opening it in notepad++ and there is no content in the file, just the filename. – bmbaeb May 16 '12 at 21:53
  • @bmbaeb: so that's your issue :-) Why don't you output file contents? – zerkms May 16 '12 at 21:54
  • @zerkms, what guessing? I was telling him how to get it opened with the application... anyway it's a moot point now. – alexis May 16 '12 at 21:59
  • You need to find out whether it is being *sent* correctly (in which case the client browser screws it up, and the problem is likely browser-specific). What browser(s) are you using? Does it arrive correctly if you fetch it via curl? If you just run the php script from the commandline, do you see the file contents? – alexis May 16 '12 at 22:04
  • @alexis I have tried Chrome and IE, neither transferred file contents. I'll try running the script command line next. – bmbaeb May 17 '12 at 13:29
  • To make sure there's nothing wrong with your program, run your php script directly (from the commandline). If there isn't, the question linked to by @Christophe (http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http) suggests that many browsers might have a problem with `&` as well. The answers mention several possible solutions. – alexis May 18 '12 at 14:19

0 Answers0