1--TEST--
2Test file_put_contents() function : usage variation - various absolute and relative paths
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--SKIPIF--
6<?php
7if(substr(PHP_OS, 0, 3) != "WIN")
8  die("skip Only run on Windows");
9?>
10--FILE--
11<?php
12echo "*** Testing file_put_contents() : usage variation ***\n";
13
14$mainDir = "filePutContentsVar7.dir";
15$subDir = "filePutContentsVar7Sub";
16$absMainDir = __DIR__."/".$mainDir;
17mkdir($absMainDir);
18$absSubDir = $absMainDir."\\".$subDir;
19mkdir($absSubDir);
20
21$old_dir_path = getcwd();
22chdir(__DIR__);
23$unixifiedDir = '/'.substr(str_replace('\\','/',$absSubDir),3);
24
25
26// Note invalid dirs in p8 result in (The system cannot find the path specified.)
27// rather than No Such File or Directory in php.net
28$allDirs = array(
29  // absolute paths
30  "$absSubDir\\",
31  "$absSubDir\\..\\".$subDir,
32  "$absSubDir\\\\..\\.\\".$subDir,
33  "$absSubDir\\..\\..\\".$mainDir."\\.\\".$subDir,
34  "$absSubDir\\..\\\\\\".$subDir."\\\\..\\\\..\\".$subDir,
35  "$absSubDir\\BADDIR",
36
37  // relative paths
38  $mainDir."\\".$subDir,
39  $mainDir."\\\\".$subDir,
40   $mainDir."\\\\\\".$subDir,
41  ".\\".$mainDir."\\..\\".$mainDir."\\".$subDir,
42  "BADDIR",
43
44  // unixifed path
45  $unixifiedDir,
46);
47
48$filename = 'FileGetContentsVar7.tmp';
49$absFile = $absSubDir.'/'.$filename;
50$data = "This was the written data";
51
52for($i = 0; $i<count($allDirs); $i++) {
53  $j = $i+1;
54  $dir = $allDirs[$i];
55  echo "\n-- Iteration $j --\n";
56  $res = file_put_contents($dir."\\".$filename, ($data . $i));
57  if ($res !== false) {
58      $in = file_get_contents($absFile);
59      if ($in == ($data . $i)) {
60         echo "Data written correctly\n";
61      }
62      else {
63         echo "Data not written correctly or to correct place\n";
64      }
65      unlink($dir."/".$filename);
66  }
67  else {
68     echo "No data written\n";
69  }
70
71}
72
73chdir($old_dir_path);
74rmdir($absSubDir);
75rmdir($absMainDir);
76
77echo "\n*** Done ***\n";
78?>
79--EXPECTF--
80*** Testing file_put_contents() : usage variation ***
81
82-- Iteration 1 --
83Data written correctly
84
85-- Iteration 2 --
86Data written correctly
87
88-- Iteration 3 --
89Data written correctly
90
91-- Iteration 4 --
92Data written correctly
93
94-- Iteration 5 --
95
96Warning: file_put_contents(%sfilePutContentsVar7.dir\filePutContentsVar7Sub\..\\\filePutContentsVar7Sub\\..\\..\filePutContentsVar7Sub\FileGetContentsVar7.tmp): Failed to open stream: %s in %s on line %d
97No data written
98
99-- Iteration 6 --
100
101Warning: file_put_contents(%sfilePutContentsVar7.dir\filePutContentsVar7Sub\BADDIR\FileGetContentsVar7.tmp): Failed to open stream: %s in %s on line %d
102No data written
103
104-- Iteration 7 --
105Data written correctly
106
107-- Iteration 8 --
108Data written correctly
109
110-- Iteration 9 --
111Data written correctly
112
113-- Iteration 10 --
114Data written correctly
115
116-- Iteration 11 --
117
118Warning: file_put_contents(BADDIR\FileGetContentsVar7.tmp): Failed to open stream: %s in %s on line %d
119No data written
120
121-- Iteration 12 --
122Data written correctly
123
124*** Done ***
125