import zend standard tests
[hiphop-php.git] / hphp / test / zend / bad / ext-standard-file / ftruncate_variation6.php
blob984912736f4446992fef8997bdc2cdcc493d2816
1 <?php
2 /*
3 Prototype: bool ftruncate ( resource $handle, int $size );
4 Description: Truncates a file to a given length
5 */
7 /* truncate the file to smaller size and display the content */
9 // include common file related test functions
10 include ("file.inc");
12 echo "*** Testing ftruncate() : usage variations ***\n";
14 /* test ftruncate with file opened in different modes */
15 $file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t",
16 "w", "wb", "wt", "w+", "w+b", "w+t",
17 "x", "xb", "xt", "x+", "x+b", "x+t",
18 "a", "ab", "at", "a+", "a+b", "a+t");
20 $file_content_types = array("numeric","text_with_new_line");
22 foreach($file_content_types as $file_content_type) {
23 echo "\n-- Testing ftruncate() with file having data of type ". $file_content_type ." --\n";
25 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
26 echo "-- Testing ftruncate() with file opening using $file_modes[$mode_counter] mode --\n";
28 // create 1 file with some contents
29 $filename = dirname(__FILE__)."/ftruncate_variation6.tmp";
30 if( strstr($file_modes[$mode_counter], "x") || strstr($file_modes[$mode_counter], "w") ) {
31 // fopen the file using the $file_modes
32 $file_handle = fopen($filename, $file_modes[$mode_counter]);
33 fill_file($file_handle, $file_content_type, 1024);
34 } else {
35 create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "ftruncate_variation", 6);
36 // fopen the file using the $file_modes
37 $file_handle = fopen($filename, $file_modes[$mode_counter]);
39 if (!$file_handle) {
40 echo "Error: failed to open file $filename!\n";
41 exit();
44 rewind($file_handle); // file pointer to 0
46 echo "-- Testing ftruncate(): truncate to smaller size and display the file content --\n";
47 /* try to truncate it and display the file content */
49 $new_size = 15;
50 var_dump( filesize($filename) ); // current filesize
51 var_dump( ftell($file_handle) );
52 if(ftruncate($file_handle, $new_size) ){// truncate it
53 echo "File content after truncating file to $new_size size : ";
54 var_dump( file_get_contents($filename) );
56 var_dump( ftell($file_handle) );
57 var_dump( feof($file_handle) );
58 fclose($file_handle);
59 clearstatcache(); // clear previous size value in cache
60 var_dump( filesize($filename) );
62 //delete all files created
63 delete_file( $filename );
64 }//end of inner for loop
65 }//end of outer foreach loop
66 echo "Done\n";