扫描功能不起作用

时间:2016-12-04 17:54:27

标签: php function

我正在尝试创建一个函数来扫描目录中的所有文件以及dir下面的dirs(后来甚至更进一步)。目前它可以看到main(指定)目录中的所有文件,但是在指定dir下面的dirs中,代码看不到所有文件。


这是我的功能:

<?php
function scanfiles($arg1){
    // Set variables
    $files = array();

    // Analyze the main dir
    $main_files = array_filter(scandir($arg1), function($files_arg1){ return is_file($files_arg1); });
    $main_folders = array_filter(scandir($arg1), function($folders_arg1){ return is_dir($folders_arg1); });

    // Remove dots from main_folders
    $main_folders = array_diff($main_folders, array('..', '.'));

    // Add all files from main_files to the files array
    foreach($main_files as $main_key => $main_value){
        $files[] = "/" . $main_value;
    }

    // Check subfolders
    foreach($main_folders as $sub1_key => $sub1_value){
        // Analyze subfolder
        $sub1_files = array_filter(scandir($arg1 . "/" . $sub1_value), function($files_arg1){ return is_file($files_arg1); });
        $sub1_folders = array_filter(scandir($arg1 . "/" . $sub1_value), function($folders_arg1){ return is_dir($folders_arg1); });

        // Add all files from sub1_files to files array
        foreach($sub1_files as $sub1_add_key => $sub1_add_value){
            $files[] = "/" . $sub1_value . "/" . $sub1_add_value;
        }
    }
    return $files;
}

foreach (scanfiles(__DIR__) as $key => $value) {
    echo "<br>" . $value;
}
?>



这是我得到的输出:

/.DS_Store
/.htaccess
/action.php
/index.php
/admin/.DS_Store
/includes/.DS_Store
/includes/.htaccess
/install/index.php



我真的不明白什么是错的,因为这应该是输出:

/.DS_Store
/.htaccess
/action.php
/index.php
/admin/.DS_Store
/admin/index.php
/admin/login.php
/includes/.DS_Store
/includes/.htaccess
/includes/config.inc.php
/install/index.php



任何知道这个问题的人都可以帮助我吗?

所有这些帮助将不胜感激!


-Jamie

1 个答案:

答案 0 :(得分:1)

所以,我现在发现问题是我在子文件夹的scandir中使用 $ folders_arg1 变量,而没有定义它应该检查子文件夹中是否有is_file。因此它会搜索文件本身的文件夹。

固定代码:

<?php
function scanfiles($arg1){
    global $sub1_value;

    // Define arg1 as a constant
    define("arg1", $arg1);

    // Set variables
    $files = array();

    // Analyze the main dir
    $main_files = array_filter(scandir($arg1), function($files_arg1){ return is_file(arg1 . "/" . $files_arg1); });
    $main_folders = array_filter(scandir($arg1), function($folders_arg1){ return is_dir(arg1 . "/" . $folders_arg1); });

    // Remove dots from main_folders
    $main_folders = array_diff($main_folders, array('..', '.'));

    // Add all files from main_files to the files array
    foreach($main_files as $main_key => $main_value){
        $files[] = "/" . $main_value;
    }

    // Check subfolders
    foreach($main_folders as $sub1_key => $sub1_value){
        // Analyze subfolder
        $sub1_files = array_filter(scandir($arg1 . "/" . $sub1_value), function($files_arg1){ return is_file(arg1 . "/" . $GLOBALS['sub1_value'] . "/" . $files_arg1); });
        $sub1_folders = array_filter(scandir($arg1 . "/" . $sub1_value), function($folders_arg1){ return is_dir(arg1 . "/" . $GLOBALS['sub1_value'] . "/" . $folders_arg1); });

        // Add all files from sub1_files to files array
        foreach($sub1_files as $sub1_add_key => $sub1_add_value){
            $files[] = "/" . $sub1_value . "/" . $sub1_add_value;
        }
    }
    return $files;
}
foreach (scanfiles(__DIR__) as $key => $value) {
    echo "<br>" . $value;
}
?>