1--TEST--
2Test fwrite() function : usage variations - a, ab, at, a+, a+b & a+t modes
3--SKIPIF--
4<?php
5if( substr(PHP_OS, 0, 3) == 'WIN' ) {
6   die('skip...Not valid for Windows');
7}
8?>
9--FILE--
10<?php
11echo "*** Testing fwrite() various  operations ***\n";
12
13// include the file.inc for Function: function delete_file($filename)
14include ("file.inc");
15
16/*
17 Test fwrite with file opened in mode : a,ab,at,a+,a+b,a+t
18 File having content of type numeric, text,text_with_new_line & alphanumeric
19*/
20
21$file_modes = array("a","ab","at","a+","a+b","a+t");
22$file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
23
24
25foreach($file_content_types as $file_content_type) {
26  echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
27
28  /* open the file using $files_modes and perform fwrite() on it */
29  foreach($file_modes as $file_mode) {
30    echo "-- Opening file in $file_mode --\n";
31
32    // create temp file and fill it content of type $file_content_type
33    $filename = __DIR__."/fwrite_variation3.tmp"; // this is name of the file
34    create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 3);
35
36    $file_handle = fopen($filename, $file_mode);
37    if(!$file_handle) {
38      echo "Error: failed to fopen() file: $filename!";
39      exit();
40    }
41
42    $data_to_be_written="";
43    fill_buffer($data_to_be_written,$file_content_type,1024);  //get the data of size 1024
44
45    /*  Write the data into the file, verify it by checking the file pointer position, eof position,
46        filesize & by displaying the content */
47
48    // append the data to the file, starting from current position of the file pointer
49    var_dump( ftell($file_handle) ); // expected: 1024
50    var_dump( fwrite($file_handle,$data_to_be_written,400) );
51    var_dump( ftell($file_handle) ); // expected: 1024 + 400
52    var_dump( feof($file_handle) );  // expected : true
53
54    /*overwrite data in middle of the file*/
55    fseek($file_handle, SEEK_SET, (1024 + 400)/2 );
56    var_dump( ftell($file_handle));  // expected: (1024 + 400)/2
57    var_dump( fwrite($file_handle, $data_to_be_written, 200) );
58    var_dump( ftell($file_handle) );
59    var_dump( feof($file_handle) );  //Expecting bool(false)
60
61    /* check the filesize and display file content */
62    // close the file, get the size and content of the file.
63    var_dump( fclose($file_handle) );
64    clearstatcache();//clears file status cache
65    var_dump( filesize($filename) );
66    var_dump(md5(file_get_contents($filename)));
67    // delete the file created
68    delete_file($filename); // delete file with name fwrite_variation3.tmp
69  } // end of inner foreach loop
70} // end of outer foreach loop
71
72echo "Done\n";
73?>
74--EXPECT--
75*** Testing fwrite() various  operations ***
76
77-- Testing fwrite() with file having content of type numeric --
78-- Opening file in a --
79int(0)
80int(400)
81int(400)
82bool(false)
83int(400)
84int(200)
85int(600)
86bool(false)
87bool(true)
88int(1624)
89string(32) "59ce5bf03b69069d00d6354bdc969ff6"
90-- Opening file in ab --
91int(0)
92int(400)
93int(400)
94bool(false)
95int(400)
96int(200)
97int(600)
98bool(false)
99bool(true)
100int(1624)
101string(32) "59ce5bf03b69069d00d6354bdc969ff6"
102-- Opening file in at --
103int(0)
104int(400)
105int(400)
106bool(false)
107int(400)
108int(200)
109int(600)
110bool(false)
111bool(true)
112int(1624)
113string(32) "59ce5bf03b69069d00d6354bdc969ff6"
114-- Opening file in a+ --
115int(0)
116int(400)
117int(400)
118bool(false)
119int(400)
120int(200)
121int(600)
122bool(false)
123bool(true)
124int(1624)
125string(32) "59ce5bf03b69069d00d6354bdc969ff6"
126-- Opening file in a+b --
127int(0)
128int(400)
129int(400)
130bool(false)
131int(400)
132int(200)
133int(600)
134bool(false)
135bool(true)
136int(1624)
137string(32) "59ce5bf03b69069d00d6354bdc969ff6"
138-- Opening file in a+t --
139int(0)
140int(400)
141int(400)
142bool(false)
143int(400)
144int(200)
145int(600)
146bool(false)
147bool(true)
148int(1624)
149string(32) "59ce5bf03b69069d00d6354bdc969ff6"
150
151-- Testing fwrite() with file having content of type text --
152-- Opening file in a --
153int(0)
154int(400)
155int(400)
156bool(false)
157int(400)
158int(200)
159int(600)
160bool(false)
161bool(true)
162int(1624)
163string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
164-- Opening file in ab --
165int(0)
166int(400)
167int(400)
168bool(false)
169int(400)
170int(200)
171int(600)
172bool(false)
173bool(true)
174int(1624)
175string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
176-- Opening file in at --
177int(0)
178int(400)
179int(400)
180bool(false)
181int(400)
182int(200)
183int(600)
184bool(false)
185bool(true)
186int(1624)
187string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
188-- Opening file in a+ --
189int(0)
190int(400)
191int(400)
192bool(false)
193int(400)
194int(200)
195int(600)
196bool(false)
197bool(true)
198int(1624)
199string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
200-- Opening file in a+b --
201int(0)
202int(400)
203int(400)
204bool(false)
205int(400)
206int(200)
207int(600)
208bool(false)
209bool(true)
210int(1624)
211string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
212-- Opening file in a+t --
213int(0)
214int(400)
215int(400)
216bool(false)
217int(400)
218int(200)
219int(600)
220bool(false)
221bool(true)
222int(1624)
223string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
224
225-- Testing fwrite() with file having content of type text_with_new_line --
226-- Opening file in a --
227int(0)
228int(400)
229int(400)
230bool(false)
231int(400)
232int(200)
233int(600)
234bool(false)
235bool(true)
236int(1624)
237string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
238-- Opening file in ab --
239int(0)
240int(400)
241int(400)
242bool(false)
243int(400)
244int(200)
245int(600)
246bool(false)
247bool(true)
248int(1624)
249string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
250-- Opening file in at --
251int(0)
252int(400)
253int(400)
254bool(false)
255int(400)
256int(200)
257int(600)
258bool(false)
259bool(true)
260int(1624)
261string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
262-- Opening file in a+ --
263int(0)
264int(400)
265int(400)
266bool(false)
267int(400)
268int(200)
269int(600)
270bool(false)
271bool(true)
272int(1624)
273string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
274-- Opening file in a+b --
275int(0)
276int(400)
277int(400)
278bool(false)
279int(400)
280int(200)
281int(600)
282bool(false)
283bool(true)
284int(1624)
285string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
286-- Opening file in a+t --
287int(0)
288int(400)
289int(400)
290bool(false)
291int(400)
292int(200)
293int(600)
294bool(false)
295bool(true)
296int(1624)
297string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
298
299-- Testing fwrite() with file having content of type alphanumeric --
300-- Opening file in a --
301int(0)
302int(400)
303int(400)
304bool(false)
305int(400)
306int(200)
307int(600)
308bool(false)
309bool(true)
310int(1624)
311string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
312-- Opening file in ab --
313int(0)
314int(400)
315int(400)
316bool(false)
317int(400)
318int(200)
319int(600)
320bool(false)
321bool(true)
322int(1624)
323string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
324-- Opening file in at --
325int(0)
326int(400)
327int(400)
328bool(false)
329int(400)
330int(200)
331int(600)
332bool(false)
333bool(true)
334int(1624)
335string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
336-- Opening file in a+ --
337int(0)
338int(400)
339int(400)
340bool(false)
341int(400)
342int(200)
343int(600)
344bool(false)
345bool(true)
346int(1624)
347string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
348-- Opening file in a+b --
349int(0)
350int(400)
351int(400)
352bool(false)
353int(400)
354int(200)
355int(600)
356bool(false)
357bool(true)
358int(1624)
359string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
360-- Opening file in a+t --
361int(0)
362int(400)
363int(400)
364bool(false)
365int(400)
366int(200)
367int(600)
368bool(false)
369bool(true)
370int(1624)
371string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
372Done
373