0

I have below source code (PHP) use to download CSV file

$file_name = date("YmdHis") . ".csv";


Header('Content-Type: text/csv');
Header("Content-disposition: attachment; filename=${file_name }");
Header("Content-type: application/octet-stream; name=${file_name }");

header('Pragma: 1');
header('Cache-control: private, max-age=60, pre-check=30');
session_cache_limiter('private_no_expire');  

$csv = $header.$contents;

if (mb_detect_encoding($csv) == 'SJIS-win') {
    $csv = mb_convert_encoding($csv, 'UTF-8', 'SJIS-win');
}
echo $csv;

exit;

With $header and $contents is read from database. This source work fine with Firefox, IE but i got problem with Quihoo360 (an browser of China called : 360安全浏览器). Instead of downloading CSV file with the content read from database, it download csv with the content is the HTML source of the displaying page.

Can someone let me know how to solve this problem.

Thank you very much.

1
  • You only need one Content-Type header. Change the first one to text/plain and remove the third one. Commented Aug 10, 2012 at 13:02

2 Answers 2

2

Instead of your content type, try setting it to :

Content-Type: text/plain

See a good list of content types.

Edit: Try this in you PHP:

header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
// echo out the csv file
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Fluffeh, i tried but it seems the problem cannot be solved with this
Hi Fluffeh, i tried but got the same result. I confirmed and saw the data selected correctly, it seems that instead of echo the selected content from database, it echo the return HTML source which is used to display on screen
i don't know why but when i add a parameter to the download URL, it resolved the issue
0

Use a content-type of application/force-download to force browsers to download your file. I think that is what you're asking for, right?

1 Comment

Hi siidheesh, the file downloaded successful but the content is incorrect. Instead of the content which is selected from DB, it downloaded HTML source of the displaying page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.