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