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