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