dongzhan1383 2018-05-29 10:32
浏览 5

如何循环文件和文件夹(包括子文件夹),并找出使用PHP的其他文件中包含的文件的出现?

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');
?>
  • 写回答

1条回答 默认 最新

  • doudu22272099831 2018-05-29 12:35
    关注

    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');
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导