-1

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');
?>
VBMali
  • 1,360
  • 3
  • 19
  • 46
  • Possible duplicate of [List all the files and folders in a Directory with PHP recursive function](https://stackoverflow.com/questions/24783862/list-all-the-files-and-folders-in-a-directory-with-php-recursive-function) – Can O' Spam May 29 '18 at 10:33
  • This is not duplicate, I need additional solution for this, please read my question completely! Need to find out the occurrences of filenames in other files as text apart from just listing the files and folders. – VBMali May 29 '18 at 10:34
  • Well - your question is not clear, the code to comment is way out of wack, you need to add desired and current behaviour, if you've not tried anything, Google it, try something and ask a proper question with an example we can use, not theory as at at that point, it's too broad and as such off topic. You have an example of a scandir function, but not something that looks at what your question is about? – Can O' Spam May 29 '18 at 10:37
  • I already have given the code which I have tried. and looking for the solution to list out the occurrences of filenames in other files content. – VBMali May 29 '18 at 10:39
  • Yes, and what have you tried to achieve that? Have you done anything other than scan the directories for the files themselves? Can you show how you are currently searching within the files for this? Can you show the error it's producing? If no to any / all of the above, try it first, come back with an actual issue, not asking someone to do it for you – Can O' Spam May 29 '18 at 10:40
  • I tried, but that code is not upto the mark that I can post here, hence not posted. In that else part: `$content = file_get_contents("$dir/$ff"); if (strpos($content, $ff) !== false) { echo "Success"; }` Means this will be in loop and that will again recursive – VBMali May 29 '18 at 10:42
  • Well, without the code - we can't help! We need to have something to work with, if it's incomplete, again, we can't help, complete it, even if it has issues, that's what we're here for, not to write it for you, I mean, come on, you've got over 800 rep - you should know this! – Can O' Spam May 29 '18 at 10:43
  • :-) Yes, I am trying. Will give updates here after coming out of the problem or in between! Thanks! – VBMali May 29 '18 at 10:47
  • @SamSwift웃 Solved. Thank you for motivating :-) – VBMali May 29 '18 at 12:35

1 Answers1

0

Solved:

<?php
function listFolderFiles($dir){
    $ffs = scandir($dir);

    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);
    // 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{
            find_string($dir, $ff);
        } 

        echo '</li>';
    }
    echo '</ol>';
}

function find_string($dir, $ff_temp){
    $ffs = scandir($dir);
    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);;
    // prevent empty ordered elements
    if (count($ffs) < 1)
        return;

    foreach($ffs as $ff){
        if(is_dir($dir.'/'.$ff)){
            find_string($dir.'/'.$ff, $ff_temp);
        }else{
            $content = file_get_contents("$dir/$ff");
            if (false !== strpos($content, $ff_temp) ) {
                echo "<br/>Usage found in: $dir/$ff";
            }
        } 
    }   
}

listFolderFiles('C:/Users/vmali/Desktop/Test-Directories');
?>
VBMali
  • 1,360
  • 3
  • 19
  • 46