Php implements the header () function for reading and writing csv files using php to implement the header () function for reading and writing csv files using the definition and usage of the header () function to send the original HTTP header to the client. It is important to realize that the header () function must be called to read and write csv files before any actual output is sent.
Use php to implement the header () function for reading and writing csv files
Definition and usage
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.3ppt.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 );