3 Prototype: array fputcsv ( resource $handle , array $fields [, string $delimiter [, string $enclosure]]] );
4 Description: Format line as CSV and write to the file pointer
7 /* Testing fputcsv() to write to a file when default enclosure value and delimiter
8 of two chars is provided */
10 echo "*** Testing fputcsv() : with default enclosure & delimiter of two chars ***\n";
12 /* the array is with three elements in it. Each element should be read as
13 1st element is delimiter, 2nd element is enclosure
14 and 3rd element is csv fields
17 array(',', '"', array('water,fruit') ),
18 array(',', '"', array('"water","fruit') ),
19 array(',', '"', array('"water","fruit"') ),
20 array(' ', '^', array('^water^ ^fruit^')),
21 array(':', '&', array('&water&:&fruit&')),
22 array('=', '=', array('=water===fruit=')),
23 array('-', '-', array('-water--fruit-air')),
24 array('-', '-', array('-water---fruit---air-')),
25 array(':', '&', array('&""""&:&"&:,:":&,&:,,,,'))
28 $file_path = dirname(__FILE__
);
29 $filename = "$file_path/fputcsv_variation13.tmp";
31 $file_modes = array ("r+", "r+b", "r+t",
37 foreach ($csv_lists as $csv_list) {
38 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++
) {
40 echo "\n-- file opened in $file_modes[$mode_counter] --\n";
41 // create the file and add the content with has csv fields
42 if ( strstr($file_modes[$mode_counter], "r") ) {
43 $file_handle = fopen($filename, "w");
45 $file_handle = fopen($filename, $file_modes[$mode_counter] );
47 if ( !$file_handle ) {
48 echo "Error: failed to create file $filename!\n";
51 $delimiter = $csv_list[0];
52 $enclosure = $csv_list[1];
53 $csv_field = $csv_list[2];
55 // write to a file in csv format
56 var_dump( fputcsv($file_handle, $csv_field, '++') );
57 // check the file pointer position and eof
58 var_dump( ftell($file_handle) );
59 var_dump( feof($file_handle) );
63 // print the file contents
64 var_dump( file_get_contents($filename) );