1--TEST--
2Test fwrite() function : usage variations - x, xb, xt, x+, x+b & x+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 : x, xb, xt, x+, x+b, x+t
18 File having content of type numeric, text,text_with_new_line & alphanumeric
19*/
20
21$file_modes = array("x","xb","xt","x+","x+b","x+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    $filename = __DIR__."/fwrite_variation4.tmp"; // this is name of the file
33
34    $file_handle = fopen($filename, $file_mode);
35    if(!$file_handle) {
36      echo "Error: failed to fopen() file: $filename!";
37      exit();
38    }
39
40    $data_to_be_written="";
41    fill_buffer($data_to_be_written,$file_content_type,1024);  //get the data of size 1024
42
43    /*  Write the data into the file, verify it by checking the file pointer position, eof position,
44        filesize & by displaying the content */
45    // write data to the file
46    var_dump( ftell($file_handle) );
47    var_dump( fwrite($file_handle,$data_to_be_written,400));
48    var_dump( ftell($file_handle) );
49    var_dump( feof($file_handle) );  // expected: true
50
51    //check the filesize and content
52    // close the file, get the size and content of the file.
53    var_dump( fclose($file_handle) );
54    clearstatcache();//clears file status cache
55    var_dump( filesize($filename) );
56    var_dump(md5(file_get_contents($filename)));
57    // delete the file created
58    delete_file($filename); // delete file with name fwrite_variation4.tmp
59  } // end of inner foreach loop
60} // end of outer foreach loop
61
62echo "Done\n";
63?>
64--EXPECT--
65*** Testing fwrite() various  operations ***
66
67-- Testing fwrite() with file having content of type numeric --
68-- Opening file in x --
69int(0)
70int(400)
71int(400)
72bool(false)
73bool(true)
74int(400)
75string(32) "f255efe87ebdf755e515868cea9ad24b"
76-- Opening file in xb --
77int(0)
78int(400)
79int(400)
80bool(false)
81bool(true)
82int(400)
83string(32) "f255efe87ebdf755e515868cea9ad24b"
84-- Opening file in xt --
85int(0)
86int(400)
87int(400)
88bool(false)
89bool(true)
90int(400)
91string(32) "f255efe87ebdf755e515868cea9ad24b"
92-- Opening file in x+ --
93int(0)
94int(400)
95int(400)
96bool(false)
97bool(true)
98int(400)
99string(32) "f255efe87ebdf755e515868cea9ad24b"
100-- Opening file in x+b --
101int(0)
102int(400)
103int(400)
104bool(false)
105bool(true)
106int(400)
107string(32) "f255efe87ebdf755e515868cea9ad24b"
108-- Opening file in x+t --
109int(0)
110int(400)
111int(400)
112bool(false)
113bool(true)
114int(400)
115string(32) "f255efe87ebdf755e515868cea9ad24b"
116
117-- Testing fwrite() with file having content of type text --
118-- Opening file in x --
119int(0)
120int(400)
121int(400)
122bool(false)
123bool(true)
124int(400)
125string(32) "c2244282eeca7c2d32d0dacf21e19432"
126-- Opening file in xb --
127int(0)
128int(400)
129int(400)
130bool(false)
131bool(true)
132int(400)
133string(32) "c2244282eeca7c2d32d0dacf21e19432"
134-- Opening file in xt --
135int(0)
136int(400)
137int(400)
138bool(false)
139bool(true)
140int(400)
141string(32) "c2244282eeca7c2d32d0dacf21e19432"
142-- Opening file in x+ --
143int(0)
144int(400)
145int(400)
146bool(false)
147bool(true)
148int(400)
149string(32) "c2244282eeca7c2d32d0dacf21e19432"
150-- Opening file in x+b --
151int(0)
152int(400)
153int(400)
154bool(false)
155bool(true)
156int(400)
157string(32) "c2244282eeca7c2d32d0dacf21e19432"
158-- Opening file in x+t --
159int(0)
160int(400)
161int(400)
162bool(false)
163bool(true)
164int(400)
165string(32) "c2244282eeca7c2d32d0dacf21e19432"
166
167-- Testing fwrite() with file having content of type text_with_new_line --
168-- Opening file in x --
169int(0)
170int(400)
171int(400)
172bool(false)
173bool(true)
174int(400)
175string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
176-- Opening file in xb --
177int(0)
178int(400)
179int(400)
180bool(false)
181bool(true)
182int(400)
183string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
184-- Opening file in xt --
185int(0)
186int(400)
187int(400)
188bool(false)
189bool(true)
190int(400)
191string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
192-- Opening file in x+ --
193int(0)
194int(400)
195int(400)
196bool(false)
197bool(true)
198int(400)
199string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
200-- Opening file in x+b --
201int(0)
202int(400)
203int(400)
204bool(false)
205bool(true)
206int(400)
207string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
208-- Opening file in x+t --
209int(0)
210int(400)
211int(400)
212bool(false)
213bool(true)
214int(400)
215string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
216
217-- Testing fwrite() with file having content of type alphanumeric --
218-- Opening file in x --
219int(0)
220int(400)
221int(400)
222bool(false)
223bool(true)
224int(400)
225string(32) "b2a123e1d84e6a03c8520aff7689219e"
226-- Opening file in xb --
227int(0)
228int(400)
229int(400)
230bool(false)
231bool(true)
232int(400)
233string(32) "b2a123e1d84e6a03c8520aff7689219e"
234-- Opening file in xt --
235int(0)
236int(400)
237int(400)
238bool(false)
239bool(true)
240int(400)
241string(32) "b2a123e1d84e6a03c8520aff7689219e"
242-- Opening file in x+ --
243int(0)
244int(400)
245int(400)
246bool(false)
247bool(true)
248int(400)
249string(32) "b2a123e1d84e6a03c8520aff7689219e"
250-- Opening file in x+b --
251int(0)
252int(400)
253int(400)
254bool(false)
255bool(true)
256int(400)
257string(32) "b2a123e1d84e6a03c8520aff7689219e"
258-- Opening file in x+t --
259int(0)
260int(400)
261int(400)
262bool(false)
263bool(true)
264int(400)
265string(32) "b2a123e1d84e6a03c8520aff7689219e"
266Done
267