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