PHP downloads files with headers

Source: Internet
Author: User
PHP csv file download with header

Ladies and gentlemen, I use the header to export csv files. I can successfully download and export files in a local test, but the files can only be directly displayed on the webpage on the server and cannot be downloaded. why?
My header is like this:

header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream");header("Content-Type: application/vnd.ms-excel");header("Content-Type: application/force-download"); header("Pragma: public"); header("Accept-Ranges: bytes"); header("Expires: 0");header('Content-Disposition: attachment;filename="data.csv"');header("Content-Transfer-Encoding: binary");header("Cache-Control: must-revalidate, post-check=0, pre-check=0");header('Cache-Control: private',false);


Reply to discussion (solution)

header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream");header("Content-Type: application/vnd.ms-excel");header("Content-Type: application/force-download"); header("Pragma: public"); header("Accept-Ranges: bytes"); header("Expires: 0");header('Content-Disposition: attachment;filename=data.csv');header("Content-Transfer-Encoding: binary");header("Cache-Control: must-revalidate, post-check=0, pre-check=0");header('Cache-Control: private',false);

Header ("Content-Description: File Transfer ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/vnd. ms-excel ");
Header ("Content-Type: application/force-download ");
Header ("Pragma: public ");
Header ("Accept-Ranges: bytes ");
Header ("Expires: 0 ");
Header ('content-Disposition: attachment; filename = data.csv ');
Header ("Content-Transfer-Encoding: binary ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ('cache-Control: private ', false );

Header ("Content-Description: File Transfer ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/vnd. ms-excel ");
Header ("Content-Type: application/force-download ");
Header ("Pragma: public ");
Header ("Accept-Ranges: bytes ");
Header ("Expires: 0 ");
Header ('content-Disposition: attachment; filename = data.csv ');
Header ("Content-Transfer-Encoding: binary ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ('cache-Control: private ', false); where has it changed?

However, the server can only be directly displayed on the webpage.

Indicates the code or csv content. I don't see any problems with the code.

However, the server can only be directly displayed on the webpage.

Indicates the code or csv content. I don't see any problems with the code.
It's CSV content.

However, the server can only be directly displayed on the webpage.

Indicates the code or csv content. I don't see any problems with the code.
The CSV content is output on the webpage. can you tell me what is going on?


However, the server can only be directly displayed on the webpage.

Indicates the code or csv content. I don't see any problems with the code.
The CSV content is output on the webpage. can you tell me what is going on?

Incomplete information. it is hard to say, check the file encoding format and whether there is a BOM header. if there is a bom header, the output may cause subsequent headers to become invalid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.