3 Prototype: array fputcsv ( resource $handle , array $fields [, string $delimiter [, string $enclosure]]] );
4 Description: Format line as CSV and write to the file pointer
8 /* Testing fputcsv() to write to a file when the field has no CSV format */
10 echo "*** Testing fputcsv() : with no CSV format in the field ***\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 $fields = array( array('water_fruit\n'),
18 array("water_fruit\n"),
22 $file_path = dirname(__FILE__
);
23 $filename = "$file_path/fputcsv_variation10.tmp";
25 $file_modes = array ("r+", "r+b", "r+t",
31 foreach ($fields as $field) {
32 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++
) {
34 echo "\n-- file opened in $file_modes[$mode_counter] --\n";
35 // create the file and add the content with has csv fields
36 if ( strstr($file_modes[$mode_counter], "r") ) {
37 $file_handle = fopen($filename, "w");
39 $file_handle = fopen($filename, $file_modes[$mode_counter] );
41 if ( !$file_handle ) {
42 echo "Error: failed to create file $filename!\n";
47 // write to a file in csv format
48 var_dump( fputcsv($file_handle, $csv_field) );
50 // check the file pointer position and eof
51 var_dump( ftell($file_handle) );
52 var_dump( feof($file_handle) );
56 // print the file contents
57 var_dump( file_get_contents($filename) );