fopen_variation5.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. --TEST--
  2. Test fopen() function : variation: use include path and stream context (absolute directories in path)
  3. --CREDITS--
  4. Dave Kelsey <d_kelsey@uk.ibm.com>
  5. --FILE--
  6. <?php
  7. /* Prototype : resource fopen(string filename, string mode [, bool use_include_path [, resource context]])
  8. * Description: Open a file or a URL and return a file pointer
  9. * Source code: ext/standard/file.c
  10. * Alias to functions:
  11. */
  12. //create the include directory structure
  13. $thisTestDir = basename(__FILE__, ".php") . ".dir";
  14. mkdir($thisTestDir);
  15. chdir($thisTestDir);
  16. $workingDir = "workdir";
  17. $filename = basename(__FILE__, ".php") . ".tmp";
  18. $scriptDir = dirname(__FILE__);
  19. $baseDir = getcwd();
  20. $secondFile = $baseDir."/dir2/".$filename;
  21. $firstFile = "../dir1/".$filename;
  22. $scriptFile = $scriptDir.'/'.$filename;
  23. $newdirs = array("dir1", "dir2", "dir3");
  24. $pathSep = ":";
  25. $newIncludePath = "";
  26. if(substr(PHP_OS, 0, 3) == 'WIN' ) {
  27. $pathSep = ";";
  28. }
  29. foreach($newdirs as $newdir) {
  30. mkdir($newdir);
  31. $newIncludePath .= $baseDir.'/'.$newdir.$pathSep;
  32. }
  33. mkdir($workingDir);
  34. chdir($workingDir);
  35. //define the files to go into these directories, create one in dir2
  36. echo "\n--- testing include path ---\n";
  37. set_include_path($newIncludePath);
  38. $modes = array("r", "r+", "rt");
  39. foreach($modes as $mode) {
  40. test_fopen($mode);
  41. }
  42. restore_include_path();
  43. // remove the directory structure
  44. chdir($baseDir);
  45. rmdir($workingDir);
  46. foreach($newdirs as $newdir) {
  47. rmdir($newdir);
  48. }
  49. chdir("..");
  50. rmdir($thisTestDir);
  51. function test_fopen($mode) {
  52. global $scriptFile, $secondFile, $firstFile, $filename;
  53. // create a file in the middle directory
  54. $h = fopen($secondFile, "w");
  55. fwrite($h, (binary) "in dir2");
  56. fclose($h);
  57. echo "\n** testing with mode=$mode **\n";
  58. // should read dir2 file
  59. $h = fopen($filename, $mode, true);
  60. fpassthru($h);
  61. fclose($h);
  62. echo "\n";
  63. //create a file in dir1
  64. $h = fopen($firstFile, "w");
  65. fwrite($h, (binary) "in dir1");
  66. fclose($h);
  67. //should now read dir1 file
  68. $h = fopen($filename, $mode, true);
  69. fpassthru($h);
  70. fclose($h);
  71. echo "\n";
  72. // create a file in working directory
  73. $h = fopen($filename, "w");
  74. fwrite($h, (binary) "in working dir");
  75. fclose($h);
  76. //should still read dir1 file
  77. $h = fopen($filename, $mode, true);
  78. fpassthru($h);
  79. fclose($h);
  80. echo "\n";
  81. unlink($firstFile);
  82. unlink($secondFile);
  83. //should read the file in working dir
  84. $h = fopen($filename, $mode, true);
  85. fpassthru($h);
  86. fclose($h);
  87. echo "\n";
  88. // create a file in the script directory
  89. $h = fopen($scriptFile, "w");
  90. fwrite($h, (binary) "in script dir");
  91. fclose($h);
  92. //should read the file in script dir
  93. $h = fopen($filename, $mode, true);
  94. fpassthru($h);
  95. fclose($h);
  96. echo "\n";
  97. //cleanup
  98. unlink($filename);
  99. unlink($scriptFile);
  100. }
  101. ?>
  102. ===DONE===
  103. --EXPECTF--
  104. --- testing include path ---
  105. ** testing with mode=r **
  106. in dir2
  107. in dir1
  108. in dir1
  109. in working dir
  110. in script dir
  111. ** testing with mode=r+ **
  112. in dir2
  113. in dir1
  114. in dir1
  115. in working dir
  116. in script dir
  117. ** testing with mode=rt **
  118. in dir2
  119. in dir1
  120. in dir1
  121. in working dir
  122. in script dir
  123. ===DONE===