1--TEST--
2Test file_put_contents() function : usage variation - various absolute and relative paths
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--FILE--
6<?php
7/* Prototype  : int file_put_contents(string file, mixed data [, int flags [, resource context]])
8 * Description: Write/Create a file with contents data and return the number of bytes written
9 * Source code: ext/standard/file.c
10 * Alias to functions:
11 */
12
13echo "*** Testing file_put_contents() : usage variation ***\n";
14
15$mainDir = "filePutContentsVar7.dir";
16$subDir = "filePutContentsVar7Sub";
17$absMainDir = __DIR__."/".$mainDir;
18mkdir($absMainDir);
19$absSubDir = $absMainDir."/".$subDir;
20mkdir($absSubDir);
21
22$old_dir_path = getcwd();
23chdir(__DIR__);
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);
45
46$filename = 'FileGetContentsVar7.tmp';
47$absFile = $absSubDir.'/'.$filename;
48$data = "This was the written data";
49
50for($i = 0; $i<count($allDirs); $i++) {
51  $j = $i+1;
52  $dir = $allDirs[$i];
53  echo "\n-- Iteration $j --\n";
54  $res = file_put_contents($dir."/".$filename, ($data . $i));
55  if ($res !== false) {
56      $in = file_get_contents($absFile);
57      if ($in == ($data . $i)) {
58         echo "Data written correctly\n";
59      }
60      else {
61         echo "Data not written correctly or to correct place\n";
62      }
63      unlink($dir."/".$filename);
64  }
65  else {
66     echo "No data written\n";
67  }
68
69}
70
71chdir($old_dir_path);
72rmdir($absSubDir);
73rmdir($absMainDir);
74
75echo "\n*** Done ***\n";
76?>
77--EXPECTF--
78*** Testing file_put_contents() : usage variation ***
79
80-- Iteration 1 --
81Data written correctly
82
83-- Iteration 2 --
84Data written correctly
85
86-- Iteration 3 --
87Data written correctly
88
89-- Iteration 4 --
90Data written correctly
91
92-- Iteration 5 --
93
94Warning: file_put_contents(%sfilePutContentsVar7.dir/filePutContentsVar7Sub/..///filePutContentsVar7Sub//..//../filePutContentsVar7Sub/FileGetContentsVar7.tmp): failed to open stream: %s in %s on line %d
95No data written
96
97-- Iteration 6 --
98
99Warning: file_put_contents(%sfilePutContentsVar7.dir/filePutContentsVar7Sub/BADDIR/FileGetContentsVar7.tmp): failed to open stream: %s in %s on line %d
100No data written
101
102-- Iteration 7 --
103Data written correctly
104
105-- Iteration 8 --
106Data written correctly
107
108-- Iteration 9 --
109Data written correctly
110
111-- Iteration 10 --
112Data written correctly
113
114-- Iteration 11 --
115
116Warning: file_put_contents(BADDIR/FileGetContentsVar7.tmp): failed to open stream: %s in %s on line %d
117No data written
118
119*** Done ***
120