xref: /PHP-8.1/ext/standard/tests/file/007_basic.phpt (revision 7aacc705)
1--TEST--
2Test fopen(), fclose() & feof() functions: basic functionality
3--FILE--
4<?php
5echo "*** Testing basic operations of fopen() and fclose() functions ***\n";
6$modes = array(
7  "w",
8  "wb",
9  "wt",
10  "w+",
11  "w+b",
12  "w+t",
13
14  "r",
15  "rb",
16  "rt",
17  "r+",
18  "r+b",
19  "r+t",
20
21  "a",
22  "ab",
23  "at",
24  "a+",
25  "a+t",
26  "a+b"
27);
28
29for( $i=0; $i<count($modes); $i++ ) {
30  echo "\n-- Iteration with mode '$modes[$i]' --\n";
31
32  $filename = __DIR__."/007_basic.tmp";
33  // check fopen()
34  $handle = fopen($filename, $modes[$i]);
35  var_dump($handle );
36  var_dump( ftell($handle) );
37  var_dump( feof($handle) );
38
39  // check fclose()
40  var_dump( fclose($handle) );
41  var_dump( $handle );
42  // confirm the closure, using ftell() and feof()
43  try {
44    var_dump( ftell($handle) );
45  } catch (TypeError $e) {
46    echo $e->getMessage(), "\n";
47  }
48  try {
49    var_dump( feof($handle) );
50  } catch (TypeError $e) {
51    echo $e->getMessage(), "\n";
52  }
53}
54
55// remove the temp file
56unlink($filename);
57
58$x_modes = array(
59  "x",
60  "xb",
61  "xt",
62  "x+",
63  "x+b",
64  "x+t"
65);
66
67for( $i=0; $i<count($x_modes); $i++ ) {
68  echo "\n-- Iteration with mode '$x_modes[$i]' --\n";
69  $handle = fopen($filename, $x_modes[$i]);
70  var_dump($handle );
71  var_dump( ftell($handle) );
72  var_dump( feof($handle) );
73
74  // check fclose()
75  var_dump( fclose($handle) );
76  var_dump( $handle );
77  // confirm the closure, using ftell() and feof()
78  try {
79    var_dump( ftell($handle) );
80  } catch (TypeError $e) {
81    echo $e->getMessage(), "\n";
82  }
83  try {
84    var_dump( feof($handle) );
85  } catch (TypeError $e) {
86    echo $e->getMessage(), "\n";
87  }
88  var_dump( $handle );
89
90  // remove the file
91  unlink( $filename );
92}
93
94echo "\n*** Done ***\n";
95?>
96--EXPECTF--
97*** Testing basic operations of fopen() and fclose() functions ***
98
99-- Iteration with mode 'w' --
100resource(%d) of type (stream)
101int(0)
102bool(false)
103bool(true)
104resource(%d) of type (Unknown)
105ftell(): supplied resource is not a valid stream resource
106feof(): supplied resource is not a valid stream resource
107
108-- Iteration with mode 'wb' --
109resource(%d) of type (stream)
110int(0)
111bool(false)
112bool(true)
113resource(%d) of type (Unknown)
114ftell(): supplied resource is not a valid stream resource
115feof(): supplied resource is not a valid stream resource
116
117-- Iteration with mode 'wt' --
118resource(%d) of type (stream)
119int(0)
120bool(false)
121bool(true)
122resource(%d) of type (Unknown)
123ftell(): supplied resource is not a valid stream resource
124feof(): supplied resource is not a valid stream resource
125
126-- Iteration with mode 'w+' --
127resource(%d) of type (stream)
128int(0)
129bool(false)
130bool(true)
131resource(%d) of type (Unknown)
132ftell(): supplied resource is not a valid stream resource
133feof(): supplied resource is not a valid stream resource
134
135-- Iteration with mode 'w+b' --
136resource(%d) of type (stream)
137int(0)
138bool(false)
139bool(true)
140resource(%d) of type (Unknown)
141ftell(): supplied resource is not a valid stream resource
142feof(): supplied resource is not a valid stream resource
143
144-- Iteration with mode 'w+t' --
145resource(%d) of type (stream)
146int(0)
147bool(false)
148bool(true)
149resource(%d) of type (Unknown)
150ftell(): supplied resource is not a valid stream resource
151feof(): supplied resource is not a valid stream resource
152
153-- Iteration with mode 'r' --
154resource(%d) of type (stream)
155int(0)
156bool(false)
157bool(true)
158resource(%d) of type (Unknown)
159ftell(): supplied resource is not a valid stream resource
160feof(): supplied resource is not a valid stream resource
161
162-- Iteration with mode 'rb' --
163resource(%d) of type (stream)
164int(0)
165bool(false)
166bool(true)
167resource(%d) of type (Unknown)
168ftell(): supplied resource is not a valid stream resource
169feof(): supplied resource is not a valid stream resource
170
171-- Iteration with mode 'rt' --
172resource(%d) of type (stream)
173int(0)
174bool(false)
175bool(true)
176resource(%d) of type (Unknown)
177ftell(): supplied resource is not a valid stream resource
178feof(): supplied resource is not a valid stream resource
179
180-- Iteration with mode 'r+' --
181resource(%d) of type (stream)
182int(0)
183bool(false)
184bool(true)
185resource(%d) of type (Unknown)
186ftell(): supplied resource is not a valid stream resource
187feof(): supplied resource is not a valid stream resource
188
189-- Iteration with mode 'r+b' --
190resource(%d) of type (stream)
191int(0)
192bool(false)
193bool(true)
194resource(%d) of type (Unknown)
195ftell(): supplied resource is not a valid stream resource
196feof(): supplied resource is not a valid stream resource
197
198-- Iteration with mode 'r+t' --
199resource(%d) of type (stream)
200int(0)
201bool(false)
202bool(true)
203resource(%d) of type (Unknown)
204ftell(): supplied resource is not a valid stream resource
205feof(): supplied resource is not a valid stream resource
206
207-- Iteration with mode 'a' --
208resource(%d) of type (stream)
209int(0)
210bool(false)
211bool(true)
212resource(%d) of type (Unknown)
213ftell(): supplied resource is not a valid stream resource
214feof(): supplied resource is not a valid stream resource
215
216-- Iteration with mode 'ab' --
217resource(%d) of type (stream)
218int(0)
219bool(false)
220bool(true)
221resource(%d) of type (Unknown)
222ftell(): supplied resource is not a valid stream resource
223feof(): supplied resource is not a valid stream resource
224
225-- Iteration with mode 'at' --
226resource(%d) of type (stream)
227int(0)
228bool(false)
229bool(true)
230resource(%d) of type (Unknown)
231ftell(): supplied resource is not a valid stream resource
232feof(): supplied resource is not a valid stream resource
233
234-- Iteration with mode 'a+' --
235resource(%d) of type (stream)
236int(0)
237bool(false)
238bool(true)
239resource(%d) of type (Unknown)
240ftell(): supplied resource is not a valid stream resource
241feof(): supplied resource is not a valid stream resource
242
243-- Iteration with mode 'a+t' --
244resource(%d) of type (stream)
245int(0)
246bool(false)
247bool(true)
248resource(%d) of type (Unknown)
249ftell(): supplied resource is not a valid stream resource
250feof(): supplied resource is not a valid stream resource
251
252-- Iteration with mode 'a+b' --
253resource(%d) of type (stream)
254int(0)
255bool(false)
256bool(true)
257resource(%d) of type (Unknown)
258ftell(): supplied resource is not a valid stream resource
259feof(): supplied resource is not a valid stream resource
260
261-- Iteration with mode 'x' --
262resource(%d) of type (stream)
263int(0)
264bool(false)
265bool(true)
266resource(%d) of type (Unknown)
267ftell(): supplied resource is not a valid stream resource
268feof(): supplied resource is not a valid stream resource
269resource(%d) of type (Unknown)
270
271-- Iteration with mode 'xb' --
272resource(%d) of type (stream)
273int(0)
274bool(false)
275bool(true)
276resource(%d) of type (Unknown)
277ftell(): supplied resource is not a valid stream resource
278feof(): supplied resource is not a valid stream resource
279resource(%d) of type (Unknown)
280
281-- Iteration with mode 'xt' --
282resource(%d) of type (stream)
283int(0)
284bool(false)
285bool(true)
286resource(%d) of type (Unknown)
287ftell(): supplied resource is not a valid stream resource
288feof(): supplied resource is not a valid stream resource
289resource(%d) of type (Unknown)
290
291-- Iteration with mode 'x+' --
292resource(%d) of type (stream)
293int(0)
294bool(false)
295bool(true)
296resource(%d) of type (Unknown)
297ftell(): supplied resource is not a valid stream resource
298feof(): supplied resource is not a valid stream resource
299resource(%d) of type (Unknown)
300
301-- Iteration with mode 'x+b' --
302resource(%d) of type (stream)
303int(0)
304bool(false)
305bool(true)
306resource(%d) of type (Unknown)
307ftell(): supplied resource is not a valid stream resource
308feof(): supplied resource is not a valid stream resource
309resource(%d) of type (Unknown)
310
311-- Iteration with mode 'x+t' --
312resource(%d) of type (stream)
313int(0)
314bool(false)
315bool(true)
316resource(%d) of type (Unknown)
317ftell(): supplied resource is not a valid stream resource
318feof(): supplied resource is not a valid stream resource
319resource(%d) of type (Unknown)
320
321*** Done ***
322