1--TEST--
2Test fwrite() function : usage variations - r+, r+b & r+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
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 : r+,r+b,r+t
27 File having content of type numeric, text,text_with_new_line & alphanumeric
28*/
29
30$file_modes = array("r+", "r+b", "r+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    // create temp file and fill the data of type $file_content_type
42    $filename = dirname(__FILE__)."/fwrite_variation2.tmp"; // this is name of the file
43    create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 2);
44
45    $file_handle = fopen($filename, $file_mode);
46    if(!$file_handle) {
47      echo "Error: failed to fopen() file: $filename!";
48      exit();
49    }
50
51    $data_to_be_written="";
52    fill_buffer($data_to_be_written,$file_content_type,1024);  //get the data of size 1024
53
54    /*  Write the data into the file, verify it by checking the file pointer position, eof position,
55        filesize & by displaying the content */
56
57    /*overwrite first 400 bytes in the file*/
58    var_dump( ftell($file_handle) );  // expected : 0
59    var_dump( fwrite($file_handle, $data_to_be_written, 400));
60    var_dump( ftell($file_handle) );  // expected: 400
61    var_dump( feof($file_handle) );  //Expecting bool(false)
62
63    /*overwrite data in middle of the file*/
64    fseek($file_handle, SEEK_SET, 1024/2 );
65    var_dump( ftell($file_handle));  // expected: 1024/2
66    var_dump( fwrite($file_handle, $data_to_be_written, 200) );
67    var_dump( ftell($file_handle) );
68    var_dump( feof($file_handle) );  //Expecting bool(false)
69
70    /* write at the end of the file */
71    fseek($file_handle, SEEK_END, 0);
72    var_dump( ftell($file_handle) );  // expected: 1024
73    var_dump( feof($file_handle) );
74    var_dump( fwrite($file_handle, $data_to_be_written, 200) );
75    var_dump( ftell($file_handle) );
76    var_dump( feof($file_handle) );  //Expecting bool(false)
77
78    /* display the file content, check the file size  */
79    var_dump( fclose($file_handle) );
80    clearstatcache();//clears file status cache
81    var_dump( filesize($filename) );
82    var_dump(md5(file_get_contents($filename)));
83    delete_file($filename); // delete file with name fwrite_variation2.tmp
84  } // end of inner foreach loop
85} // end of outer foreach loop
86
87echo "Done\n";
88?>
89--EXPECTF--
90*** Testing fwrite() various  operations ***
91
92-- Testing fwrite() with file having content of type numeric --
93-- Opening file in r+ --
94int(0)
95int(400)
96int(400)
97bool(false)
98int(400)
99int(200)
100int(600)
101bool(false)
102int(2)
103bool(false)
104int(200)
105int(202)
106bool(false)
107bool(true)
108int(1024)
109string(32) "950b7457d1deb6332f2fc5d42f3129d6"
110-- Opening file in r+b --
111int(0)
112int(400)
113int(400)
114bool(false)
115int(400)
116int(200)
117int(600)
118bool(false)
119int(2)
120bool(false)
121int(200)
122int(202)
123bool(false)
124bool(true)
125int(1024)
126string(32) "950b7457d1deb6332f2fc5d42f3129d6"
127-- Opening file in r+t --
128int(0)
129int(400)
130int(400)
131bool(false)
132int(400)
133int(200)
134int(600)
135bool(false)
136int(2)
137bool(false)
138int(200)
139int(202)
140bool(false)
141bool(true)
142int(1024)
143string(32) "950b7457d1deb6332f2fc5d42f3129d6"
144
145-- Testing fwrite() with file having content of type text --
146-- Opening file in r+ --
147int(0)
148int(400)
149int(400)
150bool(false)
151int(400)
152int(200)
153int(600)
154bool(false)
155int(2)
156bool(false)
157int(200)
158int(202)
159bool(false)
160bool(true)
161int(1024)
162string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
163-- Opening file in r+b --
164int(0)
165int(400)
166int(400)
167bool(false)
168int(400)
169int(200)
170int(600)
171bool(false)
172int(2)
173bool(false)
174int(200)
175int(202)
176bool(false)
177bool(true)
178int(1024)
179string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
180-- Opening file in r+t --
181int(0)
182int(400)
183int(400)
184bool(false)
185int(400)
186int(200)
187int(600)
188bool(false)
189int(2)
190bool(false)
191int(200)
192int(202)
193bool(false)
194bool(true)
195int(1024)
196string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
197
198-- Testing fwrite() with file having content of type text_with_new_line --
199-- Opening file in r+ --
200int(0)
201int(400)
202int(400)
203bool(false)
204int(400)
205int(200)
206int(600)
207bool(false)
208int(2)
209bool(false)
210int(200)
211int(202)
212bool(false)
213bool(true)
214int(1024)
215string(32) "b188d7c8aa229cbef067e5970f2daba9"
216-- Opening file in r+b --
217int(0)
218int(400)
219int(400)
220bool(false)
221int(400)
222int(200)
223int(600)
224bool(false)
225int(2)
226bool(false)
227int(200)
228int(202)
229bool(false)
230bool(true)
231int(1024)
232string(32) "b188d7c8aa229cbef067e5970f2daba9"
233-- Opening file in r+t --
234int(0)
235int(400)
236int(400)
237bool(false)
238int(400)
239int(200)
240int(600)
241bool(false)
242int(2)
243bool(false)
244int(200)
245int(202)
246bool(false)
247bool(true)
248int(1024)
249string(32) "b188d7c8aa229cbef067e5970f2daba9"
250
251-- Testing fwrite() with file having content of type alphanumeric --
252-- Opening file in r+ --
253int(0)
254int(400)
255int(400)
256bool(false)
257int(400)
258int(200)
259int(600)
260bool(false)
261int(2)
262bool(false)
263int(200)
264int(202)
265bool(false)
266bool(true)
267int(1024)
268string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
269-- Opening file in r+b --
270int(0)
271int(400)
272int(400)
273bool(false)
274int(400)
275int(200)
276int(600)
277bool(false)
278int(2)
279bool(false)
280int(200)
281int(202)
282bool(false)
283bool(true)
284int(1024)
285string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
286-- Opening file in r+t --
287int(0)
288int(400)
289int(400)
290bool(false)
291int(400)
292int(200)
293int(600)
294bool(false)
295int(2)
296bool(false)
297int(200)
298int(202)
299bool(false)
300bool(true)
301int(1024)
302string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
303Done
304