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 = __DIR__; 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 92 93Notice: fputcsv(): write of 12 bytes failed with errno=9 Bad file descriptor in %s on line %d 94bool(false) 95int(0) 96bool(false) 97string(0) "" 98 99-- file opened in rb -- 100 101Notice: fputcsv(): delimiter must be a single character in %s on line %d 102 103Notice: fputcsv(): enclosure must be a single character in %s on line %d 104 105Notice: fputcsv(): write of 12 bytes failed with errno=9 Bad file descriptor in %s on line %d 106bool(false) 107int(0) 108bool(false) 109string(0) "" 110 111-- file opened in rt -- 112 113Notice: fputcsv(): delimiter must be a single character in %s on line %d 114 115Notice: fputcsv(): enclosure must be a single character in %s on line %d 116 117Notice: fputcsv(): write of 12 bytes failed with errno=9 Bad file descriptor in %s on line %d 118bool(false) 119int(0) 120bool(false) 121string(0) "" 122 123-- file opened in r -- 124 125Notice: fputcsv(): delimiter must be a single character in %s on line %d 126 127Notice: fputcsv(): enclosure must be a single character in %s on line %d 128 129Notice: fputcsv(): write of 15 bytes failed with errno=9 Bad file descriptor in %s on line %d 130bool(false) 131int(0) 132bool(false) 133string(0) "" 134 135-- file opened in rb -- 136 137Notice: fputcsv(): delimiter must be a single character in %s on line %d 138 139Notice: fputcsv(): enclosure must be a single character in %s on line %d 140 141Notice: fputcsv(): write of 15 bytes failed with errno=9 Bad file descriptor in %s on line %d 142bool(false) 143int(0) 144bool(false) 145string(0) "" 146 147-- file opened in rt -- 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 152 153Notice: fputcsv(): write of 15 bytes failed with errno=9 Bad file descriptor in %s on line %d 154bool(false) 155int(0) 156bool(false) 157string(0) "" 158 159-- file opened in r -- 160 161Notice: fputcsv(): delimiter must be a single character in %s on line %d 162 163Notice: fputcsv(): enclosure must be a single character in %s on line %d 164 165Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 166bool(false) 167int(0) 168bool(false) 169string(0) "" 170 171-- file opened in rb -- 172 173Notice: fputcsv(): delimiter must be a single character in %s on line %d 174 175Notice: fputcsv(): enclosure must be a single character in %s on line %d 176 177Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 178bool(false) 179int(0) 180bool(false) 181string(0) "" 182 183-- file opened in rt -- 184 185Notice: fputcsv(): delimiter must be a single character in %s on line %d 186 187Notice: fputcsv(): enclosure must be a single character in %s on line %d 188 189Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 190bool(false) 191int(0) 192bool(false) 193string(0) "" 194 195-- file opened in r -- 196 197Notice: fputcsv(): delimiter must be a single character in %s on line %d 198 199Notice: fputcsv(): enclosure must be a single character in %s on line %d 200 201Notice: fputcsv(): write of 18 bytes failed with errno=9 Bad file descriptor in %s on line %d 202bool(false) 203int(0) 204bool(false) 205string(0) "" 206 207-- file opened in rb -- 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 212 213Notice: fputcsv(): write of 18 bytes failed with errno=9 Bad file descriptor in %s on line %d 214bool(false) 215int(0) 216bool(false) 217string(0) "" 218 219-- file opened in rt -- 220 221Notice: fputcsv(): delimiter must be a single character in %s on line %d 222 223Notice: fputcsv(): enclosure must be a single character in %s on line %d 224 225Notice: fputcsv(): write of 18 bytes failed with errno=9 Bad file descriptor in %s on line %d 226bool(false) 227int(0) 228bool(false) 229string(0) "" 230 231-- file opened in r -- 232 233Notice: fputcsv(): delimiter must be a single character in %s on line %d 234 235Notice: fputcsv(): enclosure must be a single character in %s on line %d 236 237Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 238bool(false) 239int(0) 240bool(false) 241string(0) "" 242 243-- file opened in rb -- 244 245Notice: fputcsv(): delimiter must be a single character in %s on line %d 246 247Notice: fputcsv(): enclosure must be a single character in %s on line %d 248 249Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 250bool(false) 251int(0) 252bool(false) 253string(0) "" 254 255-- file opened in rt -- 256 257Notice: fputcsv(): delimiter must be a single character in %s on line %d 258 259Notice: fputcsv(): enclosure must be a single character in %s on line %d 260 261Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 262bool(false) 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 272 273Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 274bool(false) 275int(0) 276bool(false) 277string(0) "" 278 279-- file opened in rb -- 280 281Notice: fputcsv(): delimiter must be a single character in %s on line %d 282 283Notice: fputcsv(): enclosure must be a single character in %s on line %d 284 285Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 286bool(false) 287int(0) 288bool(false) 289string(0) "" 290 291-- file opened in rt -- 292 293Notice: fputcsv(): delimiter must be a single character in %s on line %d 294 295Notice: fputcsv(): enclosure must be a single character in %s on line %d 296 297Notice: fputcsv(): write of 16 bytes failed with errno=9 Bad file descriptor in %s on line %d 298bool(false) 299int(0) 300bool(false) 301string(0) "" 302 303-- file opened in r -- 304 305Notice: fputcsv(): delimiter must be a single character in %s on line %d 306 307Notice: fputcsv(): enclosure must be a single character in %s on line %d 308 309Notice: fputcsv(): write of 18 bytes failed with errno=9 Bad file descriptor in %s on line %d 310bool(false) 311int(0) 312bool(false) 313string(0) "" 314 315-- file opened in rb -- 316 317Notice: fputcsv(): delimiter must be a single character in %s on line %d 318 319Notice: fputcsv(): enclosure must be a single character in %s on line %d 320 321Notice: fputcsv(): write of 18 bytes failed with errno=9 Bad file descriptor in %s on line %d 322bool(false) 323int(0) 324bool(false) 325string(0) "" 326 327-- file opened in rt -- 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 332 333Notice: fputcsv(): write of 18 bytes failed with errno=9 Bad file descriptor in %s on line %d 334bool(false) 335int(0) 336bool(false) 337string(0) "" 338 339-- file opened in r -- 340 341Notice: fputcsv(): delimiter must be a single character in %s on line %d 342 343Notice: fputcsv(): enclosure must be a single character in %s on line %d 344 345Notice: fputcsv(): write of 22 bytes failed with errno=9 Bad file descriptor in %s on line %d 346bool(false) 347int(0) 348bool(false) 349string(0) "" 350 351-- file opened in rb -- 352 353Notice: fputcsv(): delimiter must be a single character in %s on line %d 354 355Notice: fputcsv(): enclosure must be a single character in %s on line %d 356 357Notice: fputcsv(): write of 22 bytes failed with errno=9 Bad file descriptor in %s on line %d 358bool(false) 359int(0) 360bool(false) 361string(0) "" 362 363-- file opened in rt -- 364 365Notice: fputcsv(): delimiter must be a single character in %s on line %d 366 367Notice: fputcsv(): enclosure must be a single character in %s on line %d 368 369Notice: fputcsv(): write of 22 bytes failed with errno=9 Bad file descriptor in %s on line %d 370bool(false) 371int(0) 372bool(false) 373string(0) "" 374 375-- file opened in r -- 376 377Notice: fputcsv(): delimiter must be a single character in %s on line %d 378 379Notice: fputcsv(): enclosure must be a single character in %s on line %d 380 381Notice: fputcsv(): write of 24 bytes failed with errno=9 Bad file descriptor in %s on line %d 382bool(false) 383int(0) 384bool(false) 385string(0) "" 386 387-- file opened in rb -- 388 389Notice: fputcsv(): delimiter must be a single character in %s on line %d 390 391Notice: fputcsv(): enclosure must be a single character in %s on line %d 392 393Notice: fputcsv(): write of 24 bytes failed with errno=9 Bad file descriptor in %s on line %d 394bool(false) 395int(0) 396bool(false) 397string(0) "" 398 399-- file opened in rt -- 400 401Notice: fputcsv(): delimiter must be a single character in %s on line %d 402 403Notice: fputcsv(): enclosure must be a single character in %s on line %d 404 405Notice: fputcsv(): write of 24 bytes failed with errno=9 Bad file descriptor in %s on line %d 406bool(false) 407int(0) 408bool(false) 409string(0) "" 410Done 411