1--TEST--
2Test fgets() function : basic functionality
3--FILE--
4<?php
5/*
6 Prototype: string fgets ( resource $handle [, int $length] );
7 Description: Gets a line from file pointer
8*/
9
10// include the file.inc for common test funcitons
11include ("file.inc");
12
13$file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t");
14
15$file_content_types = array("numeric", "text", "text_with_new_line", "alphanumeric");
16
17echo "*** Testing fgets() : basic functionality ***\n";
18foreach($file_modes as $file_mode) {
19  echo "\n-- Testing fgets() with file opened using mode $file_mode --\n";
20  foreach($file_content_types as $file_content_type) {
21    echo "-- File content type : $file_content_type --\n";
22    /* create files with $file_content_type */
23    create_files ( __DIR__, 1, $file_content_type, 0755, 50, "w", "fgets_basic", 1, "bytes"); //create a file
24    $filename = __DIR__."/fgets_basic1.tmp"; // this is name of the file created by create_files()
25    $file_handle = fopen($filename, $file_mode);
26    if ( !$file_handle ) {
27      echo "Error: failed to open file $filename!";
28      exit();
29    }
30
31    echo "-- fgets() with default length, file pointer at 0 --\n";
32    var_dump( fgets($file_handle) ); // with default length
33    var_dump( ftell($file_handle) ); // ensure the file pointer position
34    var_dump( feof($file_handle) );  // enusre if eof set
35
36    echo "-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --\n";
37    var_dump( rewind($file_handle) );
38    var_dump( fgets($file_handle, 23) ); // expected: 22 chars
39    var_dump( ftell($file_handle) ); // ensure the file pointer position
40    var_dump( feof($file_handle) );  // enusre if eof set
41
42    //close file
43    fclose($file_handle);
44
45    // delete file
46    delete_file($filename);
47  } // file_content_type loop
48} // file_mode loop
49
50echo "Done\n";
51?>
52--EXPECT--
53*** Testing fgets() : basic functionality ***
54
55-- Testing fgets() with file opened using mode r --
56-- File content type : numeric --
57-- fgets() with default length, file pointer at 0 --
58string(50) "22222222222222222222222222222222222222222222222222"
59int(50)
60bool(true)
61-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
62bool(true)
63string(22) "2222222222222222222222"
64int(22)
65bool(false)
66-- File content type : text --
67-- fgets() with default length, file pointer at 0 --
68string(50) "text text text text text text text text text text "
69int(50)
70bool(true)
71-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
72bool(true)
73string(22) "text text text text te"
74int(22)
75bool(false)
76-- File content type : text_with_new_line --
77-- fgets() with default length, file pointer at 0 --
78string(5) "line
79"
80int(5)
81bool(false)
82-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
83bool(true)
84string(5) "line
85"
86int(5)
87bool(false)
88-- File content type : alphanumeric --
89-- fgets() with default length, file pointer at 0 --
90string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
91int(50)
92bool(true)
93-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
94bool(true)
95string(22) "ab12 ab12 ab12 ab12 ab"
96int(22)
97bool(false)
98
99-- Testing fgets() with file opened using mode rb --
100-- File content type : numeric --
101-- fgets() with default length, file pointer at 0 --
102string(50) "22222222222222222222222222222222222222222222222222"
103int(50)
104bool(true)
105-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
106bool(true)
107string(22) "2222222222222222222222"
108int(22)
109bool(false)
110-- File content type : text --
111-- fgets() with default length, file pointer at 0 --
112string(50) "text text text text text text text text text text "
113int(50)
114bool(true)
115-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
116bool(true)
117string(22) "text text text text te"
118int(22)
119bool(false)
120-- File content type : text_with_new_line --
121-- fgets() with default length, file pointer at 0 --
122string(5) "line
123"
124int(5)
125bool(false)
126-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
127bool(true)
128string(5) "line
129"
130int(5)
131bool(false)
132-- File content type : alphanumeric --
133-- fgets() with default length, file pointer at 0 --
134string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
135int(50)
136bool(true)
137-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
138bool(true)
139string(22) "ab12 ab12 ab12 ab12 ab"
140int(22)
141bool(false)
142
143-- Testing fgets() with file opened using mode rt --
144-- File content type : numeric --
145-- fgets() with default length, file pointer at 0 --
146string(50) "22222222222222222222222222222222222222222222222222"
147int(50)
148bool(true)
149-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
150bool(true)
151string(22) "2222222222222222222222"
152int(22)
153bool(false)
154-- File content type : text --
155-- fgets() with default length, file pointer at 0 --
156string(50) "text text text text text text text text text text "
157int(50)
158bool(true)
159-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
160bool(true)
161string(22) "text text text text te"
162int(22)
163bool(false)
164-- File content type : text_with_new_line --
165-- fgets() with default length, file pointer at 0 --
166string(5) "line
167"
168int(5)
169bool(false)
170-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
171bool(true)
172string(5) "line
173"
174int(5)
175bool(false)
176-- File content type : alphanumeric --
177-- fgets() with default length, file pointer at 0 --
178string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
179int(50)
180bool(true)
181-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
182bool(true)
183string(22) "ab12 ab12 ab12 ab12 ab"
184int(22)
185bool(false)
186
187-- Testing fgets() with file opened using mode r+ --
188-- File content type : numeric --
189-- fgets() with default length, file pointer at 0 --
190string(50) "22222222222222222222222222222222222222222222222222"
191int(50)
192bool(true)
193-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
194bool(true)
195string(22) "2222222222222222222222"
196int(22)
197bool(false)
198-- File content type : text --
199-- fgets() with default length, file pointer at 0 --
200string(50) "text text text text text text text text text text "
201int(50)
202bool(true)
203-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
204bool(true)
205string(22) "text text text text te"
206int(22)
207bool(false)
208-- File content type : text_with_new_line --
209-- fgets() with default length, file pointer at 0 --
210string(5) "line
211"
212int(5)
213bool(false)
214-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
215bool(true)
216string(5) "line
217"
218int(5)
219bool(false)
220-- File content type : alphanumeric --
221-- fgets() with default length, file pointer at 0 --
222string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
223int(50)
224bool(true)
225-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
226bool(true)
227string(22) "ab12 ab12 ab12 ab12 ab"
228int(22)
229bool(false)
230
231-- Testing fgets() with file opened using mode r+b --
232-- File content type : numeric --
233-- fgets() with default length, file pointer at 0 --
234string(50) "22222222222222222222222222222222222222222222222222"
235int(50)
236bool(true)
237-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
238bool(true)
239string(22) "2222222222222222222222"
240int(22)
241bool(false)
242-- File content type : text --
243-- fgets() with default length, file pointer at 0 --
244string(50) "text text text text text text text text text text "
245int(50)
246bool(true)
247-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
248bool(true)
249string(22) "text text text text te"
250int(22)
251bool(false)
252-- File content type : text_with_new_line --
253-- fgets() with default length, file pointer at 0 --
254string(5) "line
255"
256int(5)
257bool(false)
258-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
259bool(true)
260string(5) "line
261"
262int(5)
263bool(false)
264-- File content type : alphanumeric --
265-- fgets() with default length, file pointer at 0 --
266string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
267int(50)
268bool(true)
269-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
270bool(true)
271string(22) "ab12 ab12 ab12 ab12 ab"
272int(22)
273bool(false)
274
275-- Testing fgets() with file opened using mode r+t --
276-- File content type : numeric --
277-- fgets() with default length, file pointer at 0 --
278string(50) "22222222222222222222222222222222222222222222222222"
279int(50)
280bool(true)
281-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
282bool(true)
283string(22) "2222222222222222222222"
284int(22)
285bool(false)
286-- File content type : text --
287-- fgets() with default length, file pointer at 0 --
288string(50) "text text text text text text text text text text "
289int(50)
290bool(true)
291-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
292bool(true)
293string(22) "text text text text te"
294int(22)
295bool(false)
296-- File content type : text_with_new_line --
297-- fgets() with default length, file pointer at 0 --
298string(5) "line
299"
300int(5)
301bool(false)
302-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
303bool(true)
304string(5) "line
305"
306int(5)
307bool(false)
308-- File content type : alphanumeric --
309-- fgets() with default length, file pointer at 0 --
310string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
311int(50)
312bool(true)
313-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
314bool(true)
315string(22) "ab12 ab12 ab12 ab12 ab"
316int(22)
317bool(false)
318Done
319