PDF downloads surrounded by single quotes?

2019-07-27 11:50发布

问题:

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.

回答1:

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');


回答2:

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.