PHP using header () Read and write CSV file Method _php Tutorial

Source: Internet
Author: User
Definition and usage
The header () function sends the original HTTP header to the client.

It is important to recognize that the header () function must be called before any actual output is sent (in PHP 4 and later, you can use output caching to resolve this issue):


Result error
Output already exists before the header () is called
Header (' location:http://www.zhutiai.com/');
?> syntax
Header (string,replace,http_response_code) parameter description
string is required. Specifies the string of headers to send.
Replace is optional. Indicates whether the header replaces the previous header, or adds a second header.

The default 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. (Available in PHP 4 and later)


1. Define headers () header output format

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

Header ("Content-disposition:filename=downloaded.pdf"); Define the file name of the output, that is, set a download type, when downloading the file from the new name

Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=downloaded.pdf");

echo "1t 2t 3n"; where t is blank, n is carriage return (encoding specification cannot be output directly)
echo "1t 2t 3n";
echo "1t 2t 3n";


?>


This time you can open this PHP file, you will be prompted to download.

can also be output in table table;

Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=downloaded.pdf");
?>










t00 t01 t02
T10 T11 T12
T20 T21 T22

Write Operations for CSV:

Fputcsv () can be used for reference.

$fp = fopen (' f:/file.csv ', ' W ');
Fputcsv ($fp, Array (' AAA ', ' BBB ', ' CCCC '));
Fputcsv ($fp, Array (' mmm ', ' yyy ', ' haha ')); Fputcsv () can be implemented in the form of an array loop
Fclose ($FP);

http://www.bkjia.com/PHPjc/630751.html www.bkjia.com true http://www.bkjia.com/PHPjc/630751.html techarticle define and use the header () function to send the original HTTP header to the client. It is important to recognize that the header () function must be called before any actual output is sent (in PH ...

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