【问题标题】:How to loop through the files and folders (including sub folders) and find out the occurrences of files included in other files using php?如何使用php遍历文件和文件夹(包括子文件夹)并找出其他文件中包含的文件的出现?
【发布时间】:2018-05-29 10:32:43
【问题描述】:

如何使用php遍历文件和文件夹(包括子文件夹)并找出其他文件中包含的文件的出现?

假设,我的文件夹文件结构为:

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

输出应该是这样的:

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

有什么办法可以解决这个问题? 注意:我可以使用递归函数列出文件和文件夹子文件夹(子文件夹文件):

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

【问题讨论】:

  • 这不是重复的,我需要额外的解决方案,请完整阅读我的问题!除了列出文件和文件夹之外,还需要找出其他文件中文件名的出现情况。
  • 嗯 - 你的问题不清楚,要评论的代码太古怪了,你需要添加想要的和当前的行为,如果你没有尝试过任何东西,谷歌它,尝试一些东西然后问一个适当的问题,我们可以使用一个例子,而不是当时的理论,它太宽泛了,因此离题了。您有一个 scandir 函数的示例,但没有查看您的问题的内容?
  • 我已经给出了我尝试过的代码。并寻找解决方案来列出其他文件内容中出现的文件名。
  • 是的,您为此做了什么尝试?除了扫描文件本身的目录之外,您是否做过任何其他事情?你能说明你目前是如何在文件中搜索的吗?你能显示它产生的错误吗?如果以上任何一项/所有都不是,请先尝试,然后再返回一个实际问题,而不是要求别人为您做这件事

标签: php file-handling scandir


【解决方案1】:

已解决:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多