Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.