PHP to mimick DirectoryIndex

Posted on 2007-10-17
Last Modified: 2007-10-17
Hey there!

How can I modify this:

if ($handle = opendir('pics')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    // List all the files
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";


to display a list of links to files in the pics directory?

I'm trying to get the same functionality that DirectoryIndex *.* in a .htaccess file would provide but my host doesn't allow custom .htaccess files.

I'm also open to alternative methods that might be more elegant.  

Question by:ttist25
    LVL 20

    Accepted Solution


    $dir = "pics"; //  directory of files

    if ($handle = opendir($dir)) {
        echo "Directory handle: $handle\n<br>";
        echo "Files:\n<br>";

        // List all the files
        while (false !== ($file = readdir($handle))) {
                if($file !=".." && $file !=".") {
                      echo "<a href=".$dir."/".$file.">".$file."<br>\n";

    LVL 1

    Author Comment


    Nice job!  Thanks.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now