简体   繁体   English

扫描功能不起作用

[英]Scan function does not works

i'm trying to create a functions that scans all files in a dir and the dirs below that dir (And later going even further). 我正在尝试创建一个功能,该功能可以扫描目录中的所有文件以及该目录下的目录(以后再扫描)。 Currently it sees all files in the main (specified) dir, but in the dirs below that specified dir, the code does not sees all files. 当前,它可以看到主(指定)目录中的所有文件,但是在指定目录下的目录中,代码看不到所有文件。


This is my function: 这是我的功能:

<?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;
}
?>



This is the output i get: 这是我得到的输出:

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



I don't really understand whats going wrong cause this should be the output: 我不太了解发生了什么问题,因为这应该是输出:

/.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



Anyone that knows whats the problem, and can help me? 有什么问题的人可以帮助我吗?

All of that help would be greatly appreciated! 所有这些帮助将不胜感激!


-Jamie -杰米

Edited with corrent answare its reason is for array_filter function you dont send correct path to it if you change your code like this 使用corrent answare编辑,其原因是array_filter函数,如果您像这样更改代码,则不会向其发送正确的路径

$sub1_files = array_filter(scandir($arg1 . "/" . $sub1_value.'/'),   function($files_arg1){
        echo $files_arg1;
        return is_file($files_arg1); });

you will notice what im saying.you can handle it by your self or send path to array_filter too 您会注意到我在说什么。您可以自行处理它,也可以将路径发送到array_filter

So, i now found the problem i was using $folders_arg1 variable in the scandir of the subfolder, without defining that it should be checking if is_file in the subfolder. 因此,我现在发现问题是我在子文件夹的scandir中使用$ folders_arg1变量,而没有定义它应该检查子文件夹中的is_file。 Cause then it would search in the folder of the file itself. 原因是它会在文件本身的文件夹中搜索。

Fixed code: 固定代码:

<?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;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM