I set $userHome to C:/Users/(Users name here)/Desktop/Test
$userHome = realpath("./../../../../")."\\Desktop\\Test";
Then I get all of the names inside of the folder test inside of a array form
$testFolderArray = scandir($userHome);
Then I get the length of $testFolderArray
count = count($testFolderArray);
Then I run a for-loop for each of the folders inside of test. Inside of the for-loop it reads data.txt and properties.txt which are inside of each folder inside of the Test folder and stores content of the files inside of the two arrays.
for($x = 0; $x < $count; $x++) {
$dataFile = $testFolder."/data.txt";
$propertiesFile = $testFolder."/properties.txt";
//Reads the data inside of those files
$data = file_get_contents($dataFile);
$properties = file_get_contents($propertiesFile);
$dataArray[$x] = $data;
$propertiesArray[$x] = $properties;
}
The error I get when I run this PHP file is:
Warning: file_get_contents(C:\Users\ltrujillo\Desktop\Test/TestSub/data.txt): failed to open stream: No such file or directory i C:\Users\ltrujillo\Desktop\PHP\wamp\www\StackOverFlow_Question.php on line 30
Warning: file_get_contents(C:\Users\ltrujillo\Desktop\Test/TestSub/properties.txt): failed to open stream: No such file or directory in C:\Users\ltrujillo\Desktop\PHP\wamp\www\StackOverFlow_Question.php on line 31
I know that the issue is that it is looking for a path that is inside of the root folder with the PHP file. So how could I change it so that it can read the context of file that is out the root folder and at the path specified?
How to fix:
for($x = 0; $x < $count; $x++) {
if($testFolderArr[$x] == '.' || $testFolderArr[$x] == '..')
continue; //filter . and .. directories
$testFolder = $userHome.'\\'.$testFolderArr[$x];
$dataFile = $testFolder."\\data.txt";
$propertiesFile = $testFolder."\\properties.txt";
if(!file_exists($dataFile) || !file_exists($propertiesFile))
continue; //check for data and properties files and continue if they arent exist
//Reads the data inside of those files
$data = file_get_contents($dataFile);
$properties = file_get_contents($propertiesFile);
$dataArray[$x] = $data;
$propertiesArray[$x] = $properties;
}
My issue was that I accidentally names my file data.txt and not data so the program said that the files full name was 'data.txt.txt' which would cause a lot of errors.