1--TEST--
2Test fwrite() function : usage variations - r, rb & rt 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,rb,rt
27 File having content of type numeric, text,text_with_new_line & alphanumeric
28*/
29
30$file_modes = array("r","rb","rt");
31$file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
32
33foreach($file_content_types as $file_content_type) {
34  echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
35
36  /* open the file using $files_modes and perform fwrite() on it */
37  foreach($file_modes as $file_mode) {
38    echo "-- Opening file in $file_mode --\n";
39
40    // create the temp file with content of type $file_content_type
41    $filename = dirname(__FILE__)."/fwrite_variation1.tmp"; // this is name of the file
42    create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation");
43
44    $file_handle = fopen($filename, $file_mode);
45    if(!$file_handle) {
46      echo "Error: failed to fopen() file: $filename!";
47      exit();
48    }
49
50    $data_to_be_written="";
51    fill_buffer($data_to_be_written,$file_content_type,1024);  //get the data of size 1024
52
53    /*  Write the data into the file, verify it by checking the file pointer position, eof position,
54        filesize & by displaying the content */
55
56    var_dump( ftell($file_handle) );  // expected: 0
57    var_dump( fwrite($file_handle, $data_to_be_written ));
58    var_dump( ftell($file_handle) );  // expected: 0
59    var_dump( feof($file_handle) );  // expected: false
60
61    // move the file pointer to end of the file and try fwrite()
62    fseek($file_handle, SEEK_END, 0);
63    var_dump( ftell($file_handle) );  // expecting 1024
64    var_dump( fwrite($file_handle, $data_to_be_written) ); // fwrite to fail
65    var_dump( ftell($file_handle) );  //check that file pointer points at eof, expected: 1024
66    var_dump( feof($file_handle) );  // ensure that  feof() points to eof, expected: true
67
68    // ensure that file content/size didn't change.
69    var_dump( fclose($file_handle) );
70    clearstatcache();//clears file status cache
71    var_dump( filesize($filename) );  // expected: 1024
72    var_dump(md5(file_get_contents($filename))); // hash the output
73    delete_file($filename); // delete file with name fwrite_variation1.tmp
74  } // end of inner foreach loop
75} // end of outer foreach loop
76
77echo "Done\n";
78?>
79--EXPECTF--
80*** Testing fwrite() various  operations ***
81
82-- Testing fwrite() with file having content of type numeric --
83-- Opening file in r --
84int(0)
85int(0)
86int(0)
87bool(false)
88int(2)
89int(0)
90int(2)
91bool(false)
92bool(true)
93int(1024)
94string(32) "950b7457d1deb6332f2fc5d42f3129d6"
95-- Opening file in rb --
96int(0)
97int(0)
98int(0)
99bool(false)
100int(2)
101int(0)
102int(2)
103bool(false)
104bool(true)
105int(1024)
106string(32) "950b7457d1deb6332f2fc5d42f3129d6"
107-- Opening file in rt --
108int(0)
109int(0)
110int(0)
111bool(false)
112int(2)
113int(0)
114int(2)
115bool(false)
116bool(true)
117int(1024)
118string(32) "950b7457d1deb6332f2fc5d42f3129d6"
119
120-- Testing fwrite() with file having content of type text --
121-- Opening file in r --
122int(0)
123int(0)
124int(0)
125bool(false)
126int(2)
127int(0)
128int(2)
129bool(false)
130bool(true)
131int(1024)
132string(32) "e486000c4c8452774f746a27658d87fa"
133-- Opening file in rb --
134int(0)
135int(0)
136int(0)
137bool(false)
138int(2)
139int(0)
140int(2)
141bool(false)
142bool(true)
143int(1024)
144string(32) "e486000c4c8452774f746a27658d87fa"
145-- Opening file in rt --
146int(0)
147int(0)
148int(0)
149bool(false)
150int(2)
151int(0)
152int(2)
153bool(false)
154bool(true)
155int(1024)
156string(32) "e486000c4c8452774f746a27658d87fa"
157
158-- Testing fwrite() with file having content of type text_with_new_line --
159-- Opening file in r --
160int(0)
161int(0)
162int(0)
163bool(false)
164int(2)
165int(0)
166int(2)
167bool(false)
168bool(true)
169int(1024)
170string(32) "b09c8026a64a88d36d4c2f17983964bb"
171-- Opening file in rb --
172int(0)
173int(0)
174int(0)
175bool(false)
176int(2)
177int(0)
178int(2)
179bool(false)
180bool(true)
181int(1024)
182string(32) "b09c8026a64a88d36d4c2f17983964bb"
183-- Opening file in rt --
184int(0)
185int(0)
186int(0)
187bool(false)
188int(2)
189int(0)
190int(2)
191bool(false)
192bool(true)
193int(1024)
194string(32) "b09c8026a64a88d36d4c2f17983964bb"
195
196-- Testing fwrite() with file having content of type alphanumeric --
197-- Opening file in r --
198int(0)
199int(0)
200int(0)
201bool(false)
202int(2)
203int(0)
204int(2)
205bool(false)
206bool(true)
207int(1024)
208string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
209-- Opening file in rb --
210int(0)
211int(0)
212int(0)
213bool(false)
214int(2)
215int(0)
216int(2)
217bool(false)
218bool(true)
219int(1024)
220string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
221-- Opening file in rt --
222int(0)
223int(0)
224int(0)
225bool(false)
226int(2)
227int(0)
228int(2)
229bool(false)
230bool(true)
231int(1024)
232string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
233Done
234