007_variation3.phpt 2.6 KB

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