I've been using a PHP script to export data from my database (mysql) to a XLS file.
While the file export process is working fine on Firefox and IE.
I am getting errors when trying to export using Google Chrome.
The Google Chrome error is
Duplicate headers received from server
The response from the server contained duplicate headers. This problem is generally the result of a misconfigured website or proxy. Only the website or proxy administrator can fix this issue.
Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple distinct Content-Disposition headers received. This is disallowed to protect against HTTP response splitting attacks.
I need some assistance on this.
Thanks
I've found out what my problem was in the header section of the PHP export code. The incorrect and correct lines are as follows:
Incorrect
header("Content-Disposition: attachment;filename=\"".$this->filename."\"");
Correct
header("Content-Disposition: attachment; filename=\"".$this->filename."\"");
The correction being adding a space between attachment; and filename
Hope this helps.
I had this same problem. However appearing only very rarely. Cause was similar but not quite the same.
Incorrect
header("Content-Disposition: attachment; filename=$filename");
Correct
header("Content-Disposition: attachment; filename=\"$filename\"");
$filename sometimes contained spaces resulting in mentionec Chrome error.
I also faced the same problem. While downloading a file having comma in its name it was saying "duplicate headers received" and it is only in chrome. In Firefox it was OK. After that I just changed my code from
header("Content-Disposition: attachment; filename=$myfilename"); to
header("Content-Disposition: attachment; filename=\"$myfilename\""); and it worked fine. Hope it will work for you.
Try this may help you,
header('Content-Disposition: attachment; filename="'.$file_name.'"');
instead of
header('Content-Disposition: attachment; filename='.$file_name);
Related
I have xampp web server, and trying to download file using headers! Don't know what is wrong, but file not starting to download and not appears in browser! In http response I have the source of file!
header("Content-disposition: attachment;filename=d:\\archive\\result.csv");
header("Content-type: application/pdf");
readfile("sample.pdf");
Can any one halp me, please!
Try this
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=file:///D:\archive\result.csv' );
I am creating downloadable zip file, it works fine almost everywhere. But in Mozilla Firefox on save of this zip I get strange message that my_zip.zip is HTM file (sorry for the language, but I hope it is pretty understandable):
If I choose save option it will be saved as normal zip (no sign of HTM at all), but in "open as" section there are only programms for opening HTM
So, the question is How to make Firefox detect this zip as zip?
I am currently using this headers (set by PHP):
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$zipFileName");
header("Content-Transfer-Encoding: binary");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store");
header('Pragma: no-cache');
header("Content-length: " . filesize($zipFileName));
readfile($zipFileName);
Already tried using header("Content-Type: application/zip"); , does not work; plus application/zip is not standart (as I read here in some headers related question).
I am using Mozilla Firefox v40.0.3, the php project is using Laravel 5.1 (I doubt it has anything to do with this)
UPDATE:
While trying different application\[format]s , I added a dump and die command after headers
//bunch of kosher headers here...
readfile($zipFileName);
dd(headers_list());//dumps and dies
And I get a zip type in download window. Then I figured out that after die or exit I will always get right download type of zip; Then I deleted all dump-and-die sections , but download type remains as zip. I have no idea what i have fixed by this manipulations.
I would love to have an explanation of this strange situation
A quick google search suggests the Content-Type seems to be the culprit
header("Content-Type: application/octet-stream");
Try setting it to application/x-zip-compressed ?
ALso the comments in this bug report may be useful: https://bugzilla.mozilla.org/show_bug.cgi?id=540900
I know there are a lot of mentions of this but I have tried all the suggestions and nothing seems to work.
I have this script to force download files, but when using docx formats it downloads ok but then says the file is corrupt. However word does manage to open it ok.
Does anyone know why the docx would keep saying there corrupt. I have double checked them by ftp them down from the server and they are fine and open first time.
$documentDir = '/home/';
$file = $_GET['d'];
$fileLocation = $documentDir.$file;
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($fileLocation));
header('Content-disposition: attachment; filename="'.$file.'"');
readfile($fileLocation);
exit(0);
This script works for me.
Are you sure that all your documents are situated in /home/ ?
Don't you mean the relative path home/
If this still doesn't work, can you please tell what's the size of the downloaded docx, and open that file in a text editor like sublime_text, the error will probably be written in there.
You could try modifying disposition line and adding encoding line to ensure encoding is done correctly.
header ("Content-Disposition: attachment; filename*=UTF-8'en'$file");
header('Content-Transfer-Encoding: binary');
I am currently trying to develop a PHP application in which my server downloads a file and the user can do the same almost simultaneously. I already think about the problem "If the user downloads fastly than the server...", but it's not a problem at this moment.
To do so, I used the header and readfile functions of php. Here is my code :
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$data['name'].'";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$data['size']);
readfile($remoteFile);
I must to use the Content-length header to set the proper size of the file and not the size that is downloaded when the user clicks on the link. However, after some seconds or minutes, download is stopped and I need to restart...
If you think about a solution, even if it didn't use the header(); function, please tell me.
Thank you in advance...
I have experienced that this is directly related to maximum runtime settings, that are enforced upon you if you run with safe_mode on.
If you have the option, try setting set_time_limit(0) and see if that makes it work.
if you have your own server, you should look into the mod_xsendfile module for apache, since that is built specifically to send large files to the user.
Oh, and its stupidly easy to use
header("X-Sendfile: $path_to_somefile");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$somefile\"");
exit;
When i try to extract the zip folder that i downloaded from the server forcefully, i get the message, 'no files to extract'. But when i download the zip folder directly from the server, i am able to extract it properly. Here is the code that i used to download forcefully.
$file = 'absolute/path/to/the/zip/folder';
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-Disposition: attachment; filename=".basename($file));
header( "Content-Description: File Transfer");
#readfile($file);
Does anyone know why this is happening? Any help would be appreciated. Thanks in advance
I cannot answer the question, but here are a few ideas to try out:
Try a different mimetype, such as the generic application/octet-stream.
Attach Fiddler (or any other HTTP debugger) and look at the transfer. What do you see? Is the file being downloaded? Maybe there is an invalid Content-Length header for some reason?
As noted by #middaparka in comments, don't suppress errors from readfile().
It's tricky to tell if it's due to the headers you're using or a server issue. However, I generally use the following without any problems:
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".filesize($file));
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
exit();
As such, you might want to try the above and see if that makes any difference. (At a guess, if your connection is slow the fact that you're not supplying a Content-Length header might not be helping the situation.)
Thank you everybody for your support. I found the solution. I had used mkdir function before headers were sent. It seems this was generating some error, so error was outputted before the headers and thereby causing the problem. When i suppressed the errors using '#' operator, the problem got solved. :). Earlier, i got the following message from fiddler: "Fiddler has detected a protocol violation in session #4. The Server did not return properly formatted HTTP Headers. Maybe missing altogether (e.g. HTTP/0.9), maybe only \r\r instead of \r\n\r\n?"