/hphp/test/zend/bad/ext/standard/tests/file/fwrite_variation2-win32.php

https://github.com/tstarling/hiphop-php · PHP · 80 lines · 42 code · 18 blank · 20 comment · 1 complexity · 2b2e7e41cf107be65a6f48b11ae651ee MD5 · raw file

  1. <?php
  2. /*
  3. Prototype: int fwrite ( resource $handle,string string, [, int $length] );
  4. Description: fwrite() writes the contents of string to the file stream pointed to by handle.
  5. If the length arquement is given,writing will stop after length bytes have been
  6. written or the end of string reached, whichever comes first.
  7. fwrite() returns the number of bytes written or FALSE on error
  8. */
  9. echo "*** Testing fwrite() various operations ***\n";
  10. // include the file.inc for Function: function delete_file($filename)
  11. include ("file.inc");
  12. /*
  13. Test fwrite with file opened in mode : r+,r+b,r+t
  14. File having content of type numeric, text,text_with_new_line & alphanumeric
  15. */
  16. $file_modes = array("r+", "r+b", "r+t");
  17. $file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
  18. foreach($file_content_types as $file_content_type) {
  19. echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
  20. /* open the file using $files_modes and perform fwrite() on it */
  21. foreach($file_modes as $file_mode) {
  22. echo "-- Opening file in $file_mode --\n";
  23. // create temp file and fill the data of type $file_content_type
  24. $filename = dirname(__FILE__)."/fwrite_variation2.tmp"; // this is name of the file
  25. create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 2);
  26. $file_handle = fopen($filename, $file_mode);
  27. if(!$file_handle) {
  28. echo "Error: failed to fopen() file: $filename!";
  29. exit();
  30. }
  31. $data_to_be_written="";
  32. fill_buffer($data_to_be_written,$file_content_type,1024); //get the data of size 1024
  33. /* Write the data into the file, verify it by checking the file pointer position, eof position,
  34. filesize & by displaying the content */
  35. /*overwrite first 400 bytes in the file*/
  36. var_dump( ftell($file_handle) ); // expected : 0
  37. var_dump( fwrite($file_handle, $data_to_be_written, 400));
  38. var_dump( ftell($file_handle) ); // expected: 400
  39. var_dump( feof($file_handle) ); //Expecting bool(false)
  40. /*overwrite data in middle of the file*/
  41. fseek($file_handle, SEEK_SET, 1024/2 );
  42. var_dump( ftell($file_handle)); // expected: 1024/2
  43. var_dump( fwrite($file_handle, $data_to_be_written, 200) );
  44. var_dump( ftell($file_handle) );
  45. var_dump( feof($file_handle) ); //Expecting bool(false)
  46. /* write at the end of the file */
  47. fseek($file_handle, SEEK_END, 0);
  48. var_dump( ftell($file_handle) ); // expected: 1024
  49. var_dump( feof($file_handle) );
  50. var_dump( fwrite($file_handle, $data_to_be_written, 200) );
  51. var_dump( ftell($file_handle) );
  52. var_dump( feof($file_handle) ); //Expecting bool(false)
  53. /* display the file content, check the file size */
  54. var_dump( fclose($file_handle) );
  55. clearstatcache();//clears file status cache
  56. var_dump( filesize($filename) );
  57. var_dump(md5(file_get_contents($filename)));
  58. delete_file($filename); // delete file with name fwrite_variation2.tmp
  59. } // end of inner foreach loop
  60. } // end of outer foreach loop
  61. echo "Done\n";
  62. ?>