1--TEST-- 2Test fgetcsv() : usage variations - with default enclosure, blank line 3--FILE-- 4<?php 5/* 6 Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] ); 7 Description: Gets line from file pointer and parse for CSV fields 8*/ 9 10/* 11 Testing fgetcsv() to read a file containing blank line when provided with 12 default enclosure argument 13*/ 14 15echo "*** Testing fgetcsv() : with default enclosure, blank line ***\n"; 16 17$filename = dirname(__FILE__) . '/fgetcsv_variation21.tmp'; 18@unlink($filename); 19 20$file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t", 21 "a+", "a+b", "a+t", 22 "w+", "w+b", "w+t", 23 "x+", "x+b", "x+t"); 24 25$loop_counter = 1; 26 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) { 27 // create the file and add the content with has csv fields 28 if ( strstr($file_modes[$mode_counter], "r") ) { 29 $file_handle = fopen($filename, "w"); 30 } else { 31 $file_handle = fopen($filename, $file_modes[$mode_counter] ); 32 } 33 if ( !$file_handle ) { 34 echo "Error: failed to create file $filename!\n"; 35 exit(); 36 } 37 // write a blank line 38 fwrite($file_handle, "\n"); // blank line 39 40 // close the file if the mode to be used is read mode and re-open using read mode 41 // else rewind the file pointer to beginning of the file 42 if ( strstr($file_modes[$mode_counter], "r" ) ) { 43 fclose($file_handle); 44 $file_handle = fopen($filename, $file_modes[$mode_counter]); 45 } else { 46 // rewind the file pointer to bof 47 rewind($file_handle); 48 } 49 50 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n"; 51 52 // call fgetcsv() to parse csv fields 53 54 // read the line which is a blank line to see the working of fgetcsv 55 $fp_pos = ftell($file_handle); 56 var_dump( fgetcsv($file_handle, 1024, '+') ); 57 // check the file pointer position and if eof 58 var_dump( ftell($file_handle) ); 59 var_dump( feof($file_handle) ); 60 61 // close the file 62 fclose($file_handle); 63 //delete file 64 unlink($filename); 65 } //end of mode loop 66 67echo "Done\n"; 68?> 69--EXPECT-- 70*** Testing fgetcsv() : with default enclosure, blank line *** 71 72-- Testing fgetcsv() with file opened using r mode -- 73array(1) { 74 [0]=> 75 NULL 76} 77int(1) 78bool(false) 79 80-- Testing fgetcsv() with file opened using rb mode -- 81array(1) { 82 [0]=> 83 NULL 84} 85int(1) 86bool(false) 87 88-- Testing fgetcsv() with file opened using rt mode -- 89array(1) { 90 [0]=> 91 NULL 92} 93int(1) 94bool(false) 95 96-- Testing fgetcsv() with file opened using r+ mode -- 97array(1) { 98 [0]=> 99 NULL 100} 101int(1) 102bool(false) 103 104-- Testing fgetcsv() with file opened using r+b mode -- 105array(1) { 106 [0]=> 107 NULL 108} 109int(1) 110bool(false) 111 112-- Testing fgetcsv() with file opened using r+t mode -- 113array(1) { 114 [0]=> 115 NULL 116} 117int(1) 118bool(false) 119 120-- Testing fgetcsv() with file opened using a+ mode -- 121array(1) { 122 [0]=> 123 NULL 124} 125int(1) 126bool(false) 127 128-- Testing fgetcsv() with file opened using a+b mode -- 129array(1) { 130 [0]=> 131 NULL 132} 133int(1) 134bool(false) 135 136-- Testing fgetcsv() with file opened using a+t mode -- 137array(1) { 138 [0]=> 139 NULL 140} 141int(1) 142bool(false) 143 144-- Testing fgetcsv() with file opened using w+ mode -- 145array(1) { 146 [0]=> 147 NULL 148} 149int(1) 150bool(false) 151 152-- Testing fgetcsv() with file opened using w+b mode -- 153array(1) { 154 [0]=> 155 NULL 156} 157int(1) 158bool(false) 159 160-- Testing fgetcsv() with file opened using w+t mode -- 161array(1) { 162 [0]=> 163 NULL 164} 165int(1) 166bool(false) 167 168-- Testing fgetcsv() with file opened using x+ mode -- 169array(1) { 170 [0]=> 171 NULL 172} 173int(1) 174bool(false) 175 176-- Testing fgetcsv() with file opened using x+b mode -- 177array(1) { 178 [0]=> 179 NULL 180} 181int(1) 182bool(false) 183 184-- Testing fgetcsv() with file opened using x+t mode -- 185array(1) { 186 [0]=> 187 NULL 188} 189int(1) 190bool(false) 191Done 192