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