Php: Using header () to read and write csv files _ PHP Tutorial

Source: Internet
Author: User
Php uses header () to read and write csv files. Define and use the header () function to send the original HTTP header to the client. It is important to realize that the header () function (defined and used in PH) must be called before any actual output is sent.
The header () function sends the original HTTP header to the client.

It is important to realize that the header () function must be called before any actual output is sent (in PHP 4 and later versions, you can use the output cache to solve this problem):


// Result error
// Output already exists before header () is called
Header ('Location: http://www.zhutiai.com /');
?> Syntax
Header (string, replace, http_response_code) parameter description
String is required. Specifies the header string to be sent.
Replace is optional. Indicates whether the header replaces the previous header or adds the second header.

The default value is true (replace ). False (multiple headers of the same type are allowed ).

Http_response_code is optional. Forces the HTTP response code to the specified value. (PHP 4 and later versions are available)


1. define the header output format

Header ("Content-type: application/vnd. ms-excel"); // defines the output file type

Header ("content-Disposition: filename=downloaded.pdf"); // defines the output file name, that is, set a download type. during the download, the file is renamed.

Header ("Content-type: application/vnd. ms-excel ");
Header ("content-Disposition: filenameappsdownloaded.pdf ");

Echo "1 t 2 t 3n"; // where t is blank and n is carriage return (encoding specifications cannot be output directly)
Echo "1 t 2 t 3n ";
Echo "1 t 2 t 3n ";


?>


In this case, you can open the php file and a prompt will be displayed for download.

It can also be output as a table;

Header ("Content-type: application/vnd. ms-excel ");
Header ("content-Disposition: filenameappsdownloaded.pdf ");
?>










T00 T01 T02
T10 T11 T12
T20 T21 T22

CSV write operation:

For use of fputcsv (), refer.

$ Fp = fopen ('F:/file.csv ', 'w ');
Fputcsv ($ fp, array ('AAA', 'BBB ', 'cccccc '));
Fputcsv ($ fp, array ('mmm', 'yyy', 'hahaha'); // fputcsv () can be implemented using array loops.
Fclose ($ fp );

The response header () function sends the original HTTP header to the client. It is important to realize that the header () function (in the PH...) must be called before any actual output is sent...

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.