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 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 : 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
85  } // end of inner foreach loop
86} // end of outer foreach loop
87
88echo "Done\n";
89?>
90--EXPECT--
91*** Testing fwrite() various  operations ***
92
93-- Testing fwrite() with file having content of type numeric --
94-- Opening file in r+ --
95int(0)
96int(400)
97int(400)
98bool(false)
99int(400)
100int(200)
101int(600)
102bool(false)
103int(2)
104bool(false)
105int(200)
106int(202)
107bool(false)
108bool(true)
109int(1024)
110string(32) "950b7457d1deb6332f2fc5d42f3129d6"
111-- Opening file in r+b --
112int(0)
113int(400)
114int(400)
115bool(false)
116int(400)
117int(200)
118int(600)
119bool(false)
120int(2)
121bool(false)
122int(200)
123int(202)
124bool(false)
125bool(true)
126int(1024)
127string(32) "950b7457d1deb6332f2fc5d42f3129d6"
128-- Opening file in r+t --
129int(0)
130int(400)
131int(400)
132bool(false)
133int(400)
134int(200)
135int(600)
136bool(false)
137int(2)
138bool(false)
139int(200)
140int(202)
141bool(false)
142bool(true)
143int(1024)
144string(32) "950b7457d1deb6332f2fc5d42f3129d6"
145
146-- Testing fwrite() with file having content of type text --
147-- Opening file in r+ --
148int(0)
149int(400)
150int(400)
151bool(false)
152int(400)
153int(200)
154int(600)
155bool(false)
156int(2)
157bool(false)
158int(200)
159int(202)
160bool(false)
161bool(true)
162int(1024)
163string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
164-- Opening file in r+b --
165int(0)
166int(400)
167int(400)
168bool(false)
169int(400)
170int(200)
171int(600)
172bool(false)
173int(2)
174bool(false)
175int(200)
176int(202)
177bool(false)
178bool(true)
179int(1024)
180string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
181-- Opening file in r+t --
182int(0)
183int(400)
184int(400)
185bool(false)
186int(400)
187int(200)
188int(600)
189bool(false)
190int(2)
191bool(false)
192int(200)
193int(202)
194bool(false)
195bool(true)
196int(1024)
197string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
198
199-- Testing fwrite() with file having content of type text_with_new_line --
200-- Opening file in r+ --
201int(0)
202int(400)
203int(400)
204bool(false)
205int(400)
206int(200)
207int(600)
208bool(false)
209int(2)
210bool(false)
211int(200)
212int(202)
213bool(false)
214bool(true)
215int(1024)
216string(32) "b188d7c8aa229cbef067e5970f2daba9"
217-- Opening file in r+b --
218int(0)
219int(400)
220int(400)
221bool(false)
222int(400)
223int(200)
224int(600)
225bool(false)
226int(2)
227bool(false)
228int(200)
229int(202)
230bool(false)
231bool(true)
232int(1024)
233string(32) "b188d7c8aa229cbef067e5970f2daba9"
234-- Opening file in r+t --
235int(0)
236int(400)
237int(400)
238bool(false)
239int(400)
240int(200)
241int(600)
242bool(false)
243int(2)
244bool(false)
245int(200)
246int(202)
247bool(false)
248bool(true)
249int(1024)
250string(32) "991652c76db8d17c790c702ac0a6dc5f"
251
252-- Testing fwrite() with file having content of type alphanumeric --
253-- Opening file in r+ --
254int(0)
255int(400)
256int(400)
257bool(false)
258int(400)
259int(200)
260int(600)
261bool(false)
262int(2)
263bool(false)
264int(200)
265int(202)
266bool(false)
267bool(true)
268int(1024)
269string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
270-- Opening file in r+b --
271int(0)
272int(400)
273int(400)
274bool(false)
275int(400)
276int(200)
277int(600)
278bool(false)
279int(2)
280bool(false)
281int(200)
282int(202)
283bool(false)
284bool(true)
285int(1024)
286string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
287-- Opening file in r+t --
288int(0)
289int(400)
290int(400)
291bool(false)
292int(400)
293int(200)
294int(600)
295bool(false)
296int(2)
297bool(false)
298int(200)
299int(202)
300bool(false)
301bool(true)
302int(1024)
303string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
304Done
305