1

I am having a problem with setting up PDF download in Mozilla or Internet Explorer, as you probably got from the title. If I attempt to download something.pdf for whatever reason it becomes 'something.pdf' with the single quotes on the outside, and this of course makes it impossible to read because it is not a valid file extension. I have been looking for people experiencing the same problem, and came across very few instances, none of which had solutions that worked for me. I am pretty lost and have been trying to figure this one out for a while, and so here I am. Following is the part of the code where the download occurs. The PHP variable $OrderNumber is simply a five digit number, and $FileNameToDownload is an entire directory pointing to the location of the PDF (which I have checked to verify that is does not have single quotes).

header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename='$OrderNumber'");
header("Content-Transfer-Encoding: binary ");
header('Content-Length: ' . filesize($FileNameToDownload));
while(ob_get_level()) ob_end_clean();
flush();
readfile($FileNameToDownload);

The only browsers that there is an issue in is Mozilla and IE. In Chrome and Opera it seems to work perfectly fine. Any help at all would be appreciated, as I've been stuck for a while without moving forward. If there is an alternate way of downloading PDFs I am very open to it, but all methods I found and tried besides this one gave me security errors in the console because of where it was trying to pull the PDF from. Thank you very much, and if any more detail is needed I would be happy to provide it, but I tried to be as thorough as I could.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
Christopher M
  • 99
  • 2
  • 7
  • 2
    Mozilla is a company that makes a number of different products. The browser is called Firefox. You don’t call Chrome “Google", Edge “Microsoft” or Safari “Apple”, do you? – Quentin Jan 10 '18 at 19:50
  • You are right, I apologize if that caused confusion somehow. I feel it should have been clear enough anyways, but to avoid this in the future I will double check that I have used the appropriate name! Thank you for the response, all feedback is appreciated. – Christopher M Jan 10 '18 at 22:23
  • ... Then go and correct it in your post? There is an [edit] button right underneath. – Jongware Jan 11 '18 at 10:04

2 Answers2

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

Don't put quotes in the filename if you don't want quotes in the file name.

Firefox is just doing what you are telling it to do instead of assuming you made an error.

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

Aside:

    header('Content-Type: application/zip');
    header("Content-Type: application/force-download");
    header("Content-Type: application/download");

A document can only have one Content-Type. Why are you trying to set three?

None of them are correct anyway. You said you were serving a PDF:

header('Content-Type: application/pdf');
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Well problem solved! I feel silly for not seeing it myself, but I appreciate the help very much. Thank you sir. And in regards to the content types, there are various file extensions we use, but it is primarily PDFs, which is the only place I have seen the problem. I will fix up the code as it is a bit sloppy in some areas as you have pointed out, it was sort of "hand-me-down", so I'm figuring some of this out as I go along. Thank you again. – Christopher M Jan 10 '18 at 19:59
  • You can use double quotes there. Obviously you need to escape them inside double quotes: `header("Content-Disposition: attachment;filename=\"$OrderNumber\"");` – Rikudou_Sennin Jan 10 '18 at 20:05
  • I had the quotes in the file name and it was working until today when out of the suden/sudden Chrome started to download the file with quotes included. I'm on ASP.Net and serving a XLSX file. – Máster Mar 02 '19 at 00:36
0

When I save a file from a page opened in Google Chrome, it automatically puts single quotes around the filename. For instance, in Microsoft Dynamics when I generate a list and then export to Excel, no dialog screen comes up to change the file name anyway, so the file is saved in the assigned download folder under the name 'something.xlsx', so clicking it will cause the question to a user, with which application he/she wants to open it. In IE11 for instance, there is no such problem.