DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Chara has posted 50 posts at DZone. View Full User Profile

List Directory Contents in PHP

11.04.2012
| 4199 views |
  • submit to reddit
    <?php
/* Source: http://www.apphp.com/index.php?snippet=php-list-directory-contents */
    function list_directory_content($dir){
        if(is_dir($dir)){
            if($handle = opendir($dir)){
                while(($file = readdir($handle)) !== false){
                    if($file != '.' && $file != '..' && $file != '.htaccess'){
                        echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."\n";
                    }
                }
                closedir($handle);
            }
        }
    }
    ?>

This code allows to list the contents of any given directory.