007_variation7.phpt 2.0 KB

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