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...