how do i put all the files from a folder and subfolder into an array?

<?php

function get_all_directory_and_files($dir) {
    $results = [];
    $dh = new DirectoryIterator($dir);   
    // Dirctary object 
    foreach ($dh as $item) {
        if (!$item->isDot()) {
            if ($item->isDir()) {
                $results = array_merge($results, get_all_directory_and_files($dir . DIRECTORY_SEPARATOR . $item));
            } else {
                $results[] = $dir . DIRECTORY_SEPARATOR . $item->getFilename();
            }
        }
    }

    return $results;
}

var_dump( get_all_directory_and_files("/tmp") );

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top