1--TEST-- 2Test fputcsv() : usage variations - with enclosure & delimiter of two chars(read only modes) 3--FILE-- 4<?php 5/* 6 Prototype: array fputcsv ( resource $handle , array $fields [, string $delimiter [, string $enclosure]]] ); 7 Description: Format line as CSV and write to the file pointer 8*/ 9 10/* Testing fputcsv() to write to a file when default enclosure value and delimiter 11 of two chars is provided and file is opened in read only mode */ 12 13echo "*** Testing fputcsv() : with enclosure & delimiter of two chars and file opened in read mode ***\n"; 14 15/* the array is with three elements in it. Each element should be read as 16 1st element is delimiter, 2nd element is enclosure 17 and 3rd element is csv fields 18*/ 19$csv_lists = array ( 20 array(',', '"', array('water,fruit') ), 21 array(',', '"', array('"water","fruit') ), 22 array(',', '"', array('"water","fruit"') ), 23 array(' ', '^', array('^water^ ^fruit^')), 24 array(':', '&', array('&water&:&fruit&')), 25 array('=', '=', array('=water===fruit=')), 26 array('-', '-', array('-water--fruit-air')), 27 array('-', '-', array('-water---fruit---air-')), 28 array(':', '&', array('&""""&:&"&:,:":&,&:,,,,')) 29 30); 31$file_path = dirname(__FILE__); 32$filename = "$file_path/fputcsv_variation14.tmp"; 33 34$file_modes = array ("r", "rb", "rt"); 35 36// create the file 37$file_handle = fopen($filename, "w" ); 38if ( !$file_handle ) { 39 echo "Error: failed to create file $filename!\n"; 40 exit(); 41} 42fclose($file_handle); 43 44$loop_counter = 1; 45foreach ($csv_lists as $csv_list) { 46 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) { 47 // create the file 48 $file_handle = fopen($filename, "w" ); 49 if ( !$file_handle ) { 50 echo "Error: failed to create file $filename!\n"; 51 exit(); 52 } 53 fclose($file_handle); 54 55 echo "\n-- file opened in $file_modes[$mode_counter] --\n"; 56 // add the content with has csv fields 57 $file_handle = fopen($filename, $file_modes[$mode_counter] ); 58 if ( !$file_handle ) { 59 echo "Error: failed to create file $filename!\n"; 60 exit(); 61 } 62 $delimiter = $csv_list[0]; 63 $enclosure = $csv_list[1]; 64 $csv_field = $csv_list[2]; 65 66 // write to a file in csv format 67 var_dump( fputcsv($file_handle, $csv_field, '++', '%%') ); 68 // check the file pointer position and eof 69 var_dump( ftell($file_handle) ); 70 var_dump( feof($file_handle) ); 71 //close the file 72 fclose($file_handle); 73 74 // print the file contents 75 var_dump( file_get_contents($filename) ); 76 77 //delete file 78 unlink($filename); 79 } //end of mode loop 80} // end of foreach 81 82echo "Done\n"; 83?> 84--EXPECTF-- 85*** Testing fputcsv() : with enclosure & delimiter of two chars and file opened in read mode *** 86 87-- file opened in r -- 88 89Notice: fputcsv(): delimiter must be a single character in %s on line %d 90 91Notice: fputcsv(): enclosure must be a single character in %s on line %d 92int(0) 93int(0) 94bool(false) 95string(0) "" 96 97-- file opened in rb -- 98 99Notice: fputcsv(): delimiter must be a single character in %s on line %d 100 101Notice: fputcsv(): enclosure must be a single character in %s on line %d 102int(0) 103int(0) 104bool(false) 105string(0) "" 106 107-- file opened in rt -- 108 109Notice: fputcsv(): delimiter must be a single character in %s on line %d 110 111Notice: fputcsv(): enclosure must be a single character in %s on line %d 112int(0) 113int(0) 114bool(false) 115string(0) "" 116 117-- file opened in r -- 118 119Notice: fputcsv(): delimiter must be a single character in %s on line %d 120 121Notice: fputcsv(): enclosure must be a single character in %s on line %d 122int(0) 123int(0) 124bool(false) 125string(0) "" 126 127-- file opened in rb -- 128 129Notice: fputcsv(): delimiter must be a single character in %s on line %d 130 131Notice: fputcsv(): enclosure must be a single character in %s on line %d 132int(0) 133int(0) 134bool(false) 135string(0) "" 136 137-- file opened in rt -- 138 139Notice: fputcsv(): delimiter must be a single character in %s on line %d 140 141Notice: fputcsv(): enclosure must be a single character in %s on line %d 142int(0) 143int(0) 144bool(false) 145string(0) "" 146 147-- file opened in r -- 148 149Notice: fputcsv(): delimiter must be a single character in %s on line %d 150 151Notice: fputcsv(): enclosure must be a single character in %s on line %d 152int(0) 153int(0) 154bool(false) 155string(0) "" 156 157-- file opened in rb -- 158 159Notice: fputcsv(): delimiter must be a single character in %s on line %d 160 161Notice: fputcsv(): enclosure must be a single character in %s on line %d 162int(0) 163int(0) 164bool(false) 165string(0) "" 166 167-- file opened in rt -- 168 169Notice: fputcsv(): delimiter must be a single character in %s on line %d 170 171Notice: fputcsv(): enclosure must be a single character in %s on line %d 172int(0) 173int(0) 174bool(false) 175string(0) "" 176 177-- file opened in r -- 178 179Notice: fputcsv(): delimiter must be a single character in %s on line %d 180 181Notice: fputcsv(): enclosure must be a single character in %s on line %d 182int(0) 183int(0) 184bool(false) 185string(0) "" 186 187-- file opened in rb -- 188 189Notice: fputcsv(): delimiter must be a single character in %s on line %d 190 191Notice: fputcsv(): enclosure must be a single character in %s on line %d 192int(0) 193int(0) 194bool(false) 195string(0) "" 196 197-- file opened in rt -- 198 199Notice: fputcsv(): delimiter must be a single character in %s on line %d 200 201Notice: fputcsv(): enclosure must be a single character in %s on line %d 202int(0) 203int(0) 204bool(false) 205string(0) "" 206 207-- file opened in r -- 208 209Notice: fputcsv(): delimiter must be a single character in %s on line %d 210 211Notice: fputcsv(): enclosure must be a single character in %s on line %d 212int(0) 213int(0) 214bool(false) 215string(0) "" 216 217-- file opened in rb -- 218 219Notice: fputcsv(): delimiter must be a single character in %s on line %d 220 221Notice: fputcsv(): enclosure must be a single character in %s on line %d 222int(0) 223int(0) 224bool(false) 225string(0) "" 226 227-- file opened in rt -- 228 229Notice: fputcsv(): delimiter must be a single character in %s on line %d 230 231Notice: fputcsv(): enclosure must be a single character in %s on line %d 232int(0) 233int(0) 234bool(false) 235string(0) "" 236 237-- file opened in r -- 238 239Notice: fputcsv(): delimiter must be a single character in %s on line %d 240 241Notice: fputcsv(): enclosure must be a single character in %s on line %d 242int(0) 243int(0) 244bool(false) 245string(0) "" 246 247-- file opened in rb -- 248 249Notice: fputcsv(): delimiter must be a single character in %s on line %d 250 251Notice: fputcsv(): enclosure must be a single character in %s on line %d 252int(0) 253int(0) 254bool(false) 255string(0) "" 256 257-- file opened in rt -- 258 259Notice: fputcsv(): delimiter must be a single character in %s on line %d 260 261Notice: fputcsv(): enclosure must be a single character in %s on line %d 262int(0) 263int(0) 264bool(false) 265string(0) "" 266 267-- file opened in r -- 268 269Notice: fputcsv(): delimiter must be a single character in %s on line %d 270 271Notice: fputcsv(): enclosure must be a single character in %s on line %d 272int(0) 273int(0) 274bool(false) 275string(0) "" 276 277-- file opened in rb -- 278 279Notice: fputcsv(): delimiter must be a single character in %s on line %d 280 281Notice: fputcsv(): enclosure must be a single character in %s on line %d 282int(0) 283int(0) 284bool(false) 285string(0) "" 286 287-- file opened in rt -- 288 289Notice: fputcsv(): delimiter must be a single character in %s on line %d 290 291Notice: fputcsv(): enclosure must be a single character in %s on line %d 292int(0) 293int(0) 294bool(false) 295string(0) "" 296 297-- file opened in r -- 298 299Notice: fputcsv(): delimiter must be a single character in %s on line %d 300 301Notice: fputcsv(): enclosure must be a single character in %s on line %d 302int(0) 303int(0) 304bool(false) 305string(0) "" 306 307-- file opened in rb -- 308 309Notice: fputcsv(): delimiter must be a single character in %s on line %d 310 311Notice: fputcsv(): enclosure must be a single character in %s on line %d 312int(0) 313int(0) 314bool(false) 315string(0) "" 316 317-- file opened in rt -- 318 319Notice: fputcsv(): delimiter must be a single character in %s on line %d 320 321Notice: fputcsv(): enclosure must be a single character in %s on line %d 322int(0) 323int(0) 324bool(false) 325string(0) "" 326 327-- file opened in r -- 328 329Notice: fputcsv(): delimiter must be a single character in %s on line %d 330 331Notice: fputcsv(): enclosure must be a single character in %s on line %d 332int(0) 333int(0) 334bool(false) 335string(0) "" 336 337-- file opened in rb -- 338 339Notice: fputcsv(): delimiter must be a single character in %s on line %d 340 341Notice: fputcsv(): enclosure must be a single character in %s on line %d 342int(0) 343int(0) 344bool(false) 345string(0) "" 346 347-- file opened in rt -- 348 349Notice: fputcsv(): delimiter must be a single character in %s on line %d 350 351Notice: fputcsv(): enclosure must be a single character in %s on line %d 352int(0) 353int(0) 354bool(false) 355string(0) "" 356Done 357 358