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