PHP fputcsv()Functions


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual

Definition and Usage

The fputcsv() function formats a line as CSV and writes it to an open file.

This function returns the length of the written string. If it fails, it returns FALSE.

Syntax

fputcsv(file,fields,seperator,enclosure)

Parameters Description
file Required. Specifies the open file to write to.
fields Required. Specifies the array to get data from.
separator Optional. Sets the field delimiter (one character only), default is comma ( , ).
enclosure Optional. Sets the field enclosure (one character only), default is double quote ( " ).


Tips and Notes

Tip:See the fgetcsv() function.


Example

Example

<?php
$list
= array
(
    
"Peter,Griffin,Oslo,Norway",
    
"Glenn,Quagmire,Oslo,Norway",
);

$file = fopen("contacts.csv","w");

foreach (
$list as $line)
{
    
fputcsv($file,split(',',$line));
}

fclose($file); ?>


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual Other Extensions