007_variation19.phpt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. --TEST--
  2. Test fopen and fclose() functions - usage variations - "wb" mode
  3. --FILE--
  4. <?php
  5. /*
  6. fopen() function:
  7. Prototype: resource fopen(string $filename, string $mode
  8. [, bool $use_include_path [, resource $context]] );
  9. Description: Opens file or URL.
  10. */
  11. /*
  12. fclose() function:
  13. Prototype: bool fclose ( resource $handle );
  14. Description: Closes an open file pointer
  15. */
  16. /* Test fopen() and fclose(): Opening the file in "wb" mode,
  17. checking for the file creation, write & read operations,
  18. checking for the file pointer position,
  19. checking for the file truncation when trying to open an existing file in "wb" mode,
  20. and fclose function
  21. */
  22. $file_path = dirname(__FILE__);
  23. require($file_path."/file.inc");
  24. create_files($file_path, 1, "text_with_new_line", 0755, 20, "wb", "007_variation", 19, "bytes");
  25. $file = $file_path."/007_variation19.tmp";
  26. $string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
  27. echo "*** Test fopen() & fclose() functions: with 'wb' mode ***\n";
  28. $file_handle = fopen($file, "wb"); //opening the file "wb" mode
  29. var_dump($file_handle); //Check for the content of handle
  30. var_dump( get_resource_type($file_handle) ); //Check for the type of resource
  31. var_dump( ftell($file_handle) ); //Initial file pointer position, expected at the beginning of the file
  32. var_dump( fwrite($file_handle, $string) ); //Check for write operation; passes; expected:size of the $string
  33. var_dump( ftell($file_handle) ); //File pointer position after write operation, expected at the end of the file
  34. rewind($file_handle);
  35. var_dump( fread($file_handle, 100) ); //Check for read operation; fails; expected: empty string
  36. var_dump( ftell($file_handle) ); //File pointer position after read operation, expected at the beginning of the file
  37. var_dump( fclose($file_handle) ); //Check for close operation on the file handle
  38. var_dump( get_resource_type($file_handle) ); //Check whether resource is lost after close operation
  39. var_dump( filesize($file) ); //Check for size of existing data file before opening the file in "wb" mode again, expected: size of content
  40. clearstatcache();
  41. fclose( fopen($file, "wb") ); //Opening the existing data file again in "wb" mode
  42. var_dump( filesize($file) ); //Check for size of existing data file after opening the file in "wb" mode again, expected: 0 bytes
  43. clearstatcache();
  44. unlink($file); //Deleting the file
  45. fclose( fopen($file, "wb") ); //Opening the non-existing file in "wb" mode, which will be created
  46. var_dump( file_exists($file) ); //Check for the existence of file
  47. echo "*** Done ***\n";
  48. --CLEAN--
  49. <?php
  50. unlink(dirname(__FILE__)."/007_variation19.tmp");
  51. ?>
  52. --EXPECTF--
  53. *** Test fopen() & fclose() functions: with 'wb' mode ***
  54. resource(%d) of type (stream)
  55. string(6) "stream"
  56. int(0)
  57. int(37)
  58. int(37)
  59. string(0) ""
  60. int(0)
  61. bool(true)
  62. string(7) "Unknown"
  63. int(37)
  64. int(0)
  65. bool(true)
  66. *** Done ***