There are two problems with your code:
- You're not including the directory for the source file in the path to subsidiary files, so those files are never found.
- You're not returning the total from the function so that higher level invocations can add the total for subsidiary files
Correcting those issues, and renaming the variables to something meaningful gives this code:
function fileProcessor($filename){
if(file_exists(trim($filename))){
$total = 0;
$rows = file($filename, FILE_SKIP_EMPTY_LINES);
foreach($rows as $data) {
if(!preg_match("/.txt/i", $data)){
$num = floatval($data);
$total += $num;
}else {
$total += fileProcessor(dirname($filename)."/".trim($data));
}
}
echo $filename. ' - ' .($total)."<br>\n";
}else{
echo "File does not exist<br>\n";
}
return $total;
}
fileProcessor('text_files/first.txt');
Output:
text_files/third.txt - 3
text_files/second.txt - 8
text_files/first.txt - 15
This lists the files in the order in which the totals are finally accumulated, so the lowest levels appear first.
[Edit]
I spotted a problem with the order of results if two or more filenames appear in a file. Here's a reworked version that deals with that.
To list the files in the order in which they are encountered requires reversing the natural order. In the new version below I've placed the filenames in a $fileList array which is passed down by reference. Each new invocation of the function adds its results to the end of that array. Once processing is complete the array is displayed.
function fileProcessor( &$fileList){
// Get the last filename in the array
end($fileList);
$filename = key($fileList);
if(file_exists(trim($filename))){
// Accumulate the values
$fileList[$filename] = 0;
$rows = file($filename, FILE_SKIP_EMPTY_LINES);
foreach($rows as $data) {
if(!preg_match("/.txt/i", $data)){
$num = floatval($data);
$fileList[$filename] += $num;
}else {
// Recursive call. Add the next filename to the array
$fileList[dirname($filename)."/".trim($data)]=0;
$fileList[$filename] += fileProcessor($fileList);
}
}
} else {
$fileList[$filename]= "File does not exist: $filename";
}
// return the total for the file to add to the accumulator for the file above.
return $fileList[$filename];
}
// Place the initial file in the array
$results = ['text_files/first.txt' => 0];
// run the function
fileProcessor($results);
// display the results
foreach($results as $filename=>$total) {
echo $filename.' - '.$total."<br>\n";
}
Output:
text_files/first.txt - 15
text_files/second.txt - 8
text_files/third.txt - 3
fileProcessor('text_files/first.txt');? is that meant to be kicking off the process? if so that should probably be called outside of the functionfileProcessor('text_files/first.txt');to kick off the process$total += $num;andfileProcessor(trim($data));before posting the question here it didn't work;