007_variation5.phpt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. --TEST--
  2. Test fopen and fclose() functions - usage variations - "a" 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 "a" mode,
  17. checking for the file creation, write & read operations,
  18. checking for the file pointer position,
  19. and fclose function
  20. */
  21. $file_path = dirname(__FILE__);
  22. require($file_path."/file.inc");
  23. create_files($file_path, 1, "text_with_new_line", 0755, 20, "w", "007_variation", 5, "bytes");
  24. $file = $file_path."/007_variation5.tmp";
  25. $string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
  26. echo "*** Test fopen() & fclose() functions: with 'a' mode ***\n";
  27. $file_handle = fopen($file, "a"); //opening the file "a" mode
  28. var_dump($file_handle); //Check for the content of handle
  29. var_dump( get_resource_type($file_handle) ); //Check for the type of resource
  30. var_dump( fwrite($file_handle, $string) ); //Check for write operation; passes; expected:size of the $string
  31. rewind($file_handle);
  32. var_dump( fread($file_handle, 100) ); //Check for read operation; fails; expected: empty string
  33. var_dump( ftell($file_handle) ); //File pointer position after read operation, expected at the end of the file
  34. var_dump( fclose($file_handle) ); //Check for close operation on the file handle
  35. var_dump( get_resource_type($file_handle) ); //Check whether resource is lost after close operation
  36. var_dump( filesize($file) ); //Check that data hasn't over written; Expected: Size of (initial data + newly added data)
  37. unlink($file); //Deleting the file
  38. fclose( fopen($file, "a") ); //Opening the non-existing file in "a" mode, which will be created
  39. var_dump( file_exists($file) ); //Check for the existence of file
  40. echo "*** Done ***\n";
  41. --CLEAN--
  42. <?php
  43. unlink(dirname(__FILE__)."/007_variation5.tmp");
  44. ?>
  45. --EXPECTF--
  46. *** Test fopen() & fclose() functions: with 'a' mode ***
  47. resource(%d) of type (stream)
  48. string(6) "stream"
  49. int(37)
  50. string(0) ""
  51. int(0)
  52. bool(true)
  53. string(7) "Unknown"
  54. int(57)
  55. bool(true)
  56. *** Done ***