1--TEST-- 2Test fgetcsv() : usage variations - with default enclosure, line without any csv fields 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 line without any csv fields from a file 12 when provided with default enclosure value 13*/ 14 15echo "*** Testing fgetcsv() : with default enclosure, line without any csv fields ***\n"; 16 17$filename = __DIR__ . '/fgetcsv_variation20.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 line of text 38 fwrite($file_handle, "This is line of text without csv fields\n"); 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 without csv fields, provide delimiter and 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, line without any csv fields *** 71 72-- Testing fgetcsv() with file opened using r mode -- 73array(1) { 74 [0]=> 75 string(39) "This is line of text without csv fields" 76} 77int(40) 78bool(false) 79 80-- Testing fgetcsv() with file opened using rb mode -- 81array(1) { 82 [0]=> 83 string(39) "This is line of text without csv fields" 84} 85int(40) 86bool(false) 87 88-- Testing fgetcsv() with file opened using rt mode -- 89array(1) { 90 [0]=> 91 string(39) "This is line of text without csv fields" 92} 93int(40) 94bool(false) 95 96-- Testing fgetcsv() with file opened using r+ mode -- 97array(1) { 98 [0]=> 99 string(39) "This is line of text without csv fields" 100} 101int(40) 102bool(false) 103 104-- Testing fgetcsv() with file opened using r+b mode -- 105array(1) { 106 [0]=> 107 string(39) "This is line of text without csv fields" 108} 109int(40) 110bool(false) 111 112-- Testing fgetcsv() with file opened using r+t mode -- 113array(1) { 114 [0]=> 115 string(39) "This is line of text without csv fields" 116} 117int(40) 118bool(false) 119 120-- Testing fgetcsv() with file opened using a+ mode -- 121array(1) { 122 [0]=> 123 string(39) "This is line of text without csv fields" 124} 125int(40) 126bool(false) 127 128-- Testing fgetcsv() with file opened using a+b mode -- 129array(1) { 130 [0]=> 131 string(39) "This is line of text without csv fields" 132} 133int(40) 134bool(false) 135 136-- Testing fgetcsv() with file opened using a+t mode -- 137array(1) { 138 [0]=> 139 string(39) "This is line of text without csv fields" 140} 141int(40) 142bool(false) 143 144-- Testing fgetcsv() with file opened using w+ mode -- 145array(1) { 146 [0]=> 147 string(39) "This is line of text without csv fields" 148} 149int(40) 150bool(false) 151 152-- Testing fgetcsv() with file opened using w+b mode -- 153array(1) { 154 [0]=> 155 string(39) "This is line of text without csv fields" 156} 157int(40) 158bool(false) 159 160-- Testing fgetcsv() with file opened using w+t mode -- 161array(1) { 162 [0]=> 163 string(39) "This is line of text without csv fields" 164} 165int(40) 166bool(false) 167 168-- Testing fgetcsv() with file opened using x+ mode -- 169array(1) { 170 [0]=> 171 string(39) "This is line of text without csv fields" 172} 173int(40) 174bool(false) 175 176-- Testing fgetcsv() with file opened using x+b mode -- 177array(1) { 178 [0]=> 179 string(39) "This is line of text without csv fields" 180} 181int(40) 182bool(false) 183 184-- Testing fgetcsv() with file opened using x+t mode -- 185array(1) { 186 [0]=> 187 string(39) "This is line of text without csv fields" 188} 189int(40) 190bool(false) 191Done 192