007_variation15.phpt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. --TEST--
  2. Test fopen and fclose() functions - usage variations - "xt" mode
  3. --FILE--
  4. <?php
  5. /*
  6. fopen() function:
  7. Description: Opens file or URL.
  8. */
  9. /*
  10. fclose() function:
  11. Description: Closes an open file pointer
  12. */
  13. /* Test fopen() and fclose(): Opening the file in "xt" mode,
  14. checking for the file creation, write & read operations,
  15. checking for the file pointer position,
  16. checking for the warning msg when trying to open an existing file in "xt" mode,
  17. and fclose function
  18. */
  19. $file_path = __DIR__;
  20. $string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
  21. $file = $file_path."/007_variation15.tmp";
  22. echo "*** Test fopen() & fclose() functions: with 'xt' mode ***\n";
  23. $file_handle = fopen($file, "xt"); //opening the non-existing file in "xt" mode, file will be created
  24. var_dump($file_handle); //Check for the content of handle
  25. var_dump( get_resource_type($file_handle) ); //Check for the type of resource
  26. var_dump( ftell($file_handle) ); //Initial file pointer position, expected at the beginning of the file
  27. var_dump( fwrite($file_handle, $string) ); //Check for write operation; passes; expected:size of the $string
  28. var_dump( ftell($file_handle) ); //File pointer position after write operation, expected at the end of the file
  29. rewind($file_handle);
  30. var_dump( fread($file_handle, 100) ); //Check for read operation; fails; expected: false
  31. var_dump( ftell($file_handle) ); //File pointer position after read operation, expected at the beginning of the file
  32. var_dump( fclose($file_handle) ); //Check for close operation on the file handle
  33. var_dump( get_resource_type($file_handle) ); //Check whether resource is lost after close operation
  34. $file_handle = fopen($file, "xt"); //Opening the existing data file in 'xt' mode to check for the warning message
  35. echo "*** Done ***\n";
  36. --CLEAN--
  37. <?php
  38. unlink(__DIR__."/007_variation15.tmp");
  39. ?>
  40. --EXPECTF--
  41. *** Test fopen() & fclose() functions: with 'xt' mode ***
  42. resource(%d) of type (stream)
  43. string(6) "stream"
  44. int(0)
  45. int(37)
  46. int(37)
  47. Notice: fread(): Read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
  48. bool(false)
  49. int(0)
  50. bool(true)
  51. string(7) "Unknown"
  52. Warning: fopen(%s): Failed to open stream: File exists in %s on line %d
  53. *** Done ***