jz3
jz3

Reputation: 521

help with PHP script to list directory needs to be recursive

I have a php script that reads a directory and lists all the files/directories in link form, and it works, except I'm trying to get it to be recursive so that when I choose another directory it once again uses the script to display the files inside. Right now when I select a directory it just goes to the default apache listing....any help is appreciated.

Script:

    <?php
$dirname = '/drives/Storage/AppsOSs/';
$webdirname = '/AppsOSs'; // what the directory appears as from the web browser's point of view

$dir = opendir($dirname);
$file_list = '';

while(($file = readdir($dir)) != false) {
if(($file != '.') && ($file != '..')) {
$file_list .= "<a href=\"$webdirname/$file\">$file</a><br/>";
}
}

closedir($dir);
?>

<p>
<?=$file_list?>
</p>

Upvotes: 4

Views: 956

Answers (3)

Marco Ceppi
Marco Ceppi

Reputation: 7722

First of all your code structure looks horrid - not sure if that was a copy and paste thing or what. But as it stands you're creating a link to the directory rather than calling the script again. Try this:

<?php
$dirname = ( isset($_GET['dir']) ) ? $_GET['dir'] : '/drives/Storage/AppsOSs/';

if( !$dir = opendir($dirname) )
{
    die("Unable to open $dirname");
}

$file_list = "";

while( ($file = readdir($dir)) !== false)
{
    if( ($file != '.') && ($file != '..') )
    {
        if( is_dir($dirname . $file) )
        {
            $file_list .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?dir=" . $dirname . $file . "\">" . $file . "</a><br/>";
        }
        else
        {
            $file_list .= "<a href=\"$dirname/$file\">$file</a><br/>";
        }
    }
}

closedir($dir);
?>

<p>
<?= $file_list; ?>
</p>

You may need to tweak it slightly to work with your system. However the idea is: If it's a file it loads the File path directly into the browser, if it's a directory call the script again with the new dirname. You could elaborate further with something like this:

<?php
$dirname = ( isset($_GET['dir']) ) ? $_GET['dir'] : '/drives/Storage/AppsOSs/';

if( !$dir = opendir($dirname) )
{
    die("Unable to open $dirname");
}

$dir_arr = array();
$file_arr = array();

while( ($file = readdir($dir)) !== false)
{
    if( ($file != '.') && ($file != '..') )
    {
        if( is_dir($dirname . $file) )
        {
            $dir_arr[] = "<a href=\"" . $_SERVER['PHP_SELF'] . "?dir=" . $dirname . $file . "\">" . $file . "</a>";
        }
        else
        {
            $file_arr[] = "<a href=\"$dirname/$file\">$file</a>";
        }
    }
}

closedir($dir);

$dir_list = implode("<br/>", $dir_arr);
$file_list = implode("<br/>", $file_arr);
?>

<p>
<?= "<h1>Directories</h1>" . $dir_list . "<h1>Files</h1>" . $file_list; ?>
</p>

With this setup all directories will be listed first - rather than mixed alphabetically like in the first example.

Upvotes: 2

Lizard
Lizard

Reputation: 45062

Take a look at the Directory Iterator and the Recursive Iterator

http://php.net/manual/en/class.directoryiterator.php

or even the Recursive Directory Iterator

http://www.php.net/manual/en/class.recursivedirectoryiterator.php

Upvotes: 3

Fidi
Fidi

Reputation: 5824

Try the RecursiveIterator-Class and the DirectoryIterator which are delivered by the SPL (Standard PHP Library). It comes with PHP, so you can view the documentation on php.net.

See this link:

Upvotes: 0

Related Questions