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