007_variation4.phpt 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --TEST--
  2. Test fopen and fclose() functions - usage variations - "w+" 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 "w+" 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 "w+" 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, "w", "007_variation", 4, "bytes");
  25. $file = $file_path."/007_variation4.tmp";
  26. $string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
  27. echo "*** Test fopen() & fclose() functions: with 'w+' mode ***\n";
  28. $file_handle = fopen($file, "w+"); //opening the file "w+" 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; passes; expected: content of file
  36. var_dump( ftell($file_handle) ); //File pointer position after read operation, expected at the end 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 "w+" mode again, expected: size of content
  40. clearstatcache();
  41. fclose( fopen($file, "w+") ); //Opening the existing data file again in "w+" mode
  42. var_dump( filesize($file) ); //Check for size of existing data file after opening the file in "w+" mode again, expected: 0 bytes
  43. clearstatcache();
  44. unlink($file); //Deleting the file
  45. fclose( fopen($file, "w+") ); //Opening the non-existing file in "w+" 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_variation4.tmp");
  51. ?>
  52. --EXPECTF--
  53. *** Test fopen() & fclose() functions: with 'w+' mode ***
  54. resource(%d) of type (stream)
  55. string(6) "stream"
  56. int(0)
  57. int(37)
  58. int(37)
  59. string(37) "abcdefghij
  60. mnopqrst uvwxyz
  61. 0123456789"
  62. int(37)
  63. bool(true)
  64. string(7) "Unknown"
  65. int(37)
  66. int(0)
  67. bool(true)
  68. *** Done ***