简体   繁体   中英

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

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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