Jump to content

Folder output


rondog

Recommended Posts

Using recursion. Here's a simpl(e|ish) script I wrote awhile back for a challenge on another forum.

 

function display_contents_ordered($path) {
    // vars
    $dirs = array();
    $files = array();
    
    // read directory
    $content = scandir($path);
    
    // loop through contents
    foreach($content as $file) {
        // ignore loop up commands
        if($file != '.' && $file != '..') {
            // split files and directories
            if(is_dir($file)) {
                array_push($dirs, $file);
            } else {
                array_push($files, $file);
            }
        }
    }

    // output directory
    echo "<li class='dir'>" . $path . "</li>\r\n";
    echo "<ul>\r\n";
    
    // loop directories
    foreach($dirs as $v) {
        display_contents_ordered($v);
    }
    
    // output files
    if(count($files) != 0) {
        foreach($files as $v) {
            echo "<li class='file'>" . $v . "</li>";
        }
    } else {
        echo "<li class='no_files'>-- No Files --</li>";
    }
    echo "</ul>\r\n";
}

Link to comment
https://forums.phpfreaks.com/topic/185406-folder-output/#findComment-978774
Share on other sites

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/something')); 
foreach ($iterator as $item) {
    if ($iterator->isDot()) continue;
    echo $item->getPathname() . PHP_EOL;
}

 

Ahh thats perfect..one question..what is PHP_EOL ?

Link to comment
https://forums.phpfreaks.com/topic/185406-folder-output/#findComment-978843
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.