007_variation7.phpt 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. --TEST--
  2. Test fopen and fclose() functions - usage variations - "x" 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 "x" mode,
  17. checking for the file creation, write & read operations,
  18. checking for the file pointer position,
  19. checking for the warning msg when trying to open an existing file in "x" mode,
  20. and fclose function
  21. */
  22. $file_path = dirname(__FILE__);
  23. $string = b"abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
  24. $file = $file_path."/007_variation7.tmp";
  25. echo "*** Test fopen() & fclose() functions: with 'x' mode ***\n";
  26. $file_handle = fopen($file, "x"); //opening the non-existing file in "x" mode, file will be created
  27. var_dump($file_handle); //Check for the content of handle
  28. var_dump( get_resource_type($file_handle) ); //Check for the type of resource
  29. var_dump( ftell($file_handle) ); //Initial file pointer position, expected at the beginning of the file
  30. var_dump( fwrite($file_handle, $string) ); //Check for write operation; passes; expected:size of the $string
  31. var_dump( ftell($file_handle) ); //File pointer position after write operation, expected at the end of the file
  32. rewind($file_handle);
  33. var_dump( fread($file_handle, 100) ); //Check for read operation; fails; expected: empty string
  34. var_dump( ftell($file_handle) ); //File pointer position after read operation, expected at the beginning of the file
  35. var_dump( fclose($file_handle) ); //Check for close operation on the file handle
  36. var_dump( get_resource_type($file_handle) ); //Check whether resource is lost after close operation
  37. $file_handle = fopen($file, "x"); //Opening the existing data file in 'x' mode to check for the warning message
  38. echo "*** Done ***\n";
  39. --CLEAN--
  40. <?php
  41. unlink(dirname(__FILE__)."/007_variation7.tmp");
  42. ?>
  43. --EXPECTF--
  44. *** Test fopen() & fclose() functions: with 'x' mode ***
  45. resource(%d) of type (stream)
  46. %unicode|string%(6) "stream"
  47. int(0)
  48. int(37)
  49. int(37)
  50. string(0) ""
  51. int(0)
  52. bool(true)
  53. %unicode|string%(7) "Unknown"
  54. Warning: fopen(%s): failed to open stream: File exists in %s on line %s
  55. *** Done ***