1--TEST--
2Test fwrite() function : usage variations - a, ab, at, a+, a+b & a+t mode
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 : a,ab,at,a+,a+b,a+
27 File having content of type numeric, text,text_with_new_line & alphanumeric
28*/
29
30$file_modes = array("a","ab","at","a+","a+b","a+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 it content of type $file_content_type
42    $filename = __DIR__."/fwrite_variation3.tmp"; // this is name of the file
43    create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 3);
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    // append the data to the file, starting from current position of the file pointer
57    var_dump( ftell($file_handle) ); // expected: 1024
58    var_dump( fwrite($file_handle,$data_to_be_written,400) );
59    var_dump( ftell($file_handle) ); // expected: 1024 + 400
60    var_dump( feof($file_handle) );  // expected : true
61
62    /*overwrite data in middle of the file*/
63    fseek($file_handle, SEEK_SET, (1024 + 400)/2 );
64    var_dump( ftell($file_handle));  // expected: (1024 + 400)/2
65    var_dump( fwrite($file_handle, $data_to_be_written, 200) );
66    var_dump( ftell($file_handle) );
67    var_dump( feof($file_handle) );  //Expecting bool(false)
68
69    /* check the filesize and display file content */
70    // close the file, get the size and content of the file.
71    var_dump( fclose($file_handle) );
72    clearstatcache();//clears file status cache
73    var_dump( filesize($filename) );
74    var_dump(md5(file_get_contents($filename)));
75    // delete the file created
76    delete_file($filename); // delete file with name fwrite_variation3.tmp
77  } // end of inner foreach loop
78} // end of outer foreach loop
79
80echo "Done\n";
81?>
82--EXPECT--
83*** Testing fwrite() various  operations ***
84
85-- Testing fwrite() with file having content of type numeric --
86-- Opening file in a --
87int(0)
88int(400)
89int(400)
90bool(false)
91int(400)
92int(200)
93int(600)
94bool(false)
95bool(true)
96int(1624)
97string(32) "59ce5bf03b69069d00d6354bdc969ff6"
98-- Opening file in ab --
99int(0)
100int(400)
101int(400)
102bool(false)
103int(400)
104int(200)
105int(600)
106bool(false)
107bool(true)
108int(1624)
109string(32) "59ce5bf03b69069d00d6354bdc969ff6"
110-- Opening file in at --
111int(0)
112int(400)
113int(400)
114bool(false)
115int(400)
116int(200)
117int(600)
118bool(false)
119bool(true)
120int(1624)
121string(32) "59ce5bf03b69069d00d6354bdc969ff6"
122-- Opening file in a+ --
123int(0)
124int(400)
125int(400)
126bool(false)
127int(400)
128int(200)
129int(600)
130bool(false)
131bool(true)
132int(1624)
133string(32) "59ce5bf03b69069d00d6354bdc969ff6"
134-- Opening file in a+b --
135int(0)
136int(400)
137int(400)
138bool(false)
139int(400)
140int(200)
141int(600)
142bool(false)
143bool(true)
144int(1624)
145string(32) "59ce5bf03b69069d00d6354bdc969ff6"
146-- Opening file in a+t --
147int(0)
148int(400)
149int(400)
150bool(false)
151int(400)
152int(200)
153int(600)
154bool(false)
155bool(true)
156int(1624)
157string(32) "59ce5bf03b69069d00d6354bdc969ff6"
158
159-- Testing fwrite() with file having content of type text --
160-- Opening file in a --
161int(0)
162int(400)
163int(400)
164bool(false)
165int(400)
166int(200)
167int(600)
168bool(false)
169bool(true)
170int(1624)
171string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
172-- Opening file in ab --
173int(0)
174int(400)
175int(400)
176bool(false)
177int(400)
178int(200)
179int(600)
180bool(false)
181bool(true)
182int(1624)
183string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
184-- Opening file in at --
185int(0)
186int(400)
187int(400)
188bool(false)
189int(400)
190int(200)
191int(600)
192bool(false)
193bool(true)
194int(1624)
195string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
196-- Opening file in a+ --
197int(0)
198int(400)
199int(400)
200bool(false)
201int(400)
202int(200)
203int(600)
204bool(false)
205bool(true)
206int(1624)
207string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
208-- Opening file in a+b --
209int(0)
210int(400)
211int(400)
212bool(false)
213int(400)
214int(200)
215int(600)
216bool(false)
217bool(true)
218int(1624)
219string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
220-- Opening file in a+t --
221int(0)
222int(400)
223int(400)
224bool(false)
225int(400)
226int(200)
227int(600)
228bool(false)
229bool(true)
230int(1624)
231string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
232
233-- Testing fwrite() with file having content of type text_with_new_line --
234-- Opening file in a --
235int(0)
236int(400)
237int(400)
238bool(false)
239int(400)
240int(200)
241int(600)
242bool(false)
243bool(true)
244int(1624)
245string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
246-- Opening file in ab --
247int(0)
248int(400)
249int(400)
250bool(false)
251int(400)
252int(200)
253int(600)
254bool(false)
255bool(true)
256int(1624)
257string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
258-- Opening file in at --
259int(0)
260int(400)
261int(400)
262bool(false)
263int(400)
264int(200)
265int(600)
266bool(false)
267bool(true)
268int(1690)
269string(32) "656648355b64df6fded53b12fb355ab8"
270-- Opening file in a+ --
271int(0)
272int(400)
273int(400)
274bool(false)
275int(400)
276int(200)
277int(600)
278bool(false)
279bool(true)
280int(1624)
281string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
282-- Opening file in a+b --
283int(0)
284int(400)
285int(400)
286bool(false)
287int(400)
288int(200)
289int(600)
290bool(false)
291bool(true)
292int(1624)
293string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
294-- Opening file in a+t --
295int(0)
296int(400)
297int(400)
298bool(false)
299int(400)
300int(200)
301int(600)
302bool(false)
303bool(true)
304int(1690)
305string(32) "656648355b64df6fded53b12fb355ab8"
306
307-- Testing fwrite() with file having content of type alphanumeric --
308-- Opening file in a --
309int(0)
310int(400)
311int(400)
312bool(false)
313int(400)
314int(200)
315int(600)
316bool(false)
317bool(true)
318int(1624)
319string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
320-- Opening file in ab --
321int(0)
322int(400)
323int(400)
324bool(false)
325int(400)
326int(200)
327int(600)
328bool(false)
329bool(true)
330int(1624)
331string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
332-- Opening file in at --
333int(0)
334int(400)
335int(400)
336bool(false)
337int(400)
338int(200)
339int(600)
340bool(false)
341bool(true)
342int(1624)
343string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
344-- Opening file in a+ --
345int(0)
346int(400)
347int(400)
348bool(false)
349int(400)
350int(200)
351int(600)
352bool(false)
353bool(true)
354int(1624)
355string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
356-- Opening file in a+b --
357int(0)
358int(400)
359int(400)
360bool(false)
361int(400)
362int(200)
363int(600)
364bool(false)
365bool(true)
366int(1624)
367string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
368-- Opening file in a+t --
369int(0)
370int(400)
371int(400)
372bool(false)
373int(400)
374int(200)
375int(600)
376bool(false)
377bool(true)
378int(1624)
379string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
380Done
381