How to loop through the files and folders (including sub folders) and find out the occurrences of files included in other files using php?
Suppose, my Folder-Files structure as:
MainFolder
file1.txt
content = file2.txt file3.txt
file2.txt
content = file1.txt file3.txt
file3.txt
file4.txt
SubFolder
file5.txt
content = file1.txt file2.txt
file6.txt
file7.txt
SubSubFolder
file8.txt
file9.txt
content = file10.txt file1.txt
file10.txt
Output should be like this:
MainFolder
file1.txt
=> this filename is exist in file2.txt, file5.txt, file9.txt
file2.txt
=> this filename is exist in file1.txt, file5.txt
file3.txt
=> this filename is exist in file1.txt, file2.txt
file4.txt
SubFolder
file5.txt
file6.txt
file7.txt
SubSubFolder
file8.txt
file9.txt
file10.txt
=> this filename is exist in file9.txt
What can be solution for this? Note: I am able to list the files and folders-sub folders (sub folder files) using recursive function as:
<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);
$temp_ffs = $ffs;
// prevent empty ordered elements
if (count($ffs) < 1)
return;
echo '<ol>';
foreach($ffs as $ff){
echo '<li>'.$ff;
if(is_dir($dir.'/'.$ff)){
listFolderFiles($dir.'/'.$ff);
}else{
// calling string match function : here I will be calling another function to find out the occurrences ( what will be the solution?)
}
echo '</li>';
}
echo '</ol>';
}
listFolderFiles('C:\Users\vmali\Desktop\Test-Directories');
?>