Viper Creations
Comeback Contest
Check out our forums for the an explosive comeback contest. We are giving away everything from iPods to Visual Studio. more...
»User: »Password:   Remember Me? 
Webmaster Forum - Viper Creations / Programming / PHP/MySQL / List files and directories
Posted:  08 Nov 2006 23:14
In this tutorial I will show you how to list files in a directory in a pretty way.

This can be a nice way to make files available in a simple way via a web site. There a some security stuff that you need to think about when making stuff like this public. But the way this script works is harmful :-)

We have some helpful functions in PHP for this...

First of all, for security we check that the provided directory really is a directory. For this we have the function is_dir. Pretty simple right.

The next step is maybe a little bit stranger: opendir. This opens a "handler" for the directory. This handler is used for other functions that works against the file system. So it's pretty good to have!

The most complicated row in the script is this one:

while(false !== ($file = readdir($dir)))

In english, this row will run the code inside the { }'s as long as readdir returns files. As long as there are more  files in the directory we will get the file name in the variable $file.

In some cases it is not interested in showing directories in our listing, so I have created a "config variable $listDirectories    = true; so you can decide if you want to show directories.
This row takes care of if we are to show text about directories:

if($listDirectories || $type != "dir")

Enough talking... Here is the magic code for you :-)

Set the variable $theDirectory to whatever directory you want to show. The directory "." is the current directory, and ".." is the parent directory.
Code:


<?

// This is the directory to list files for.
$theDirectory            = ".";
// Do you want to show directories? change to false to hide directories.
$listDirectories    = true;

if(is_dir($theDirectory))
{
    echo "<table><tr><td>Name</td><td>Type</td><td>Size</td></tr>";
    $dir = opendir($theDirectory);
    while(false !== ($file = readdir($dir)))
    {
        $type    = filetype($theDirectory ."/". $file);
        if($listDirectories || $type != "dir")
        {
            echo "<tr><td>" . $file . "</td>";
            echo "<td>" . $type . "</td>";
            echo "<td>";
            if($type == "file")
                echo filesize($file);
            echo "</td></tr>";
        }
    }
    closedir($dir);
    echo "</table>";
}
else
{
    echo $theDirectory . " is not a directory";
}
?>

I don't think there are more things to say about this script. It shows you the files, directories and their size in a simple table. Should be a perfect PHP function to start with for creating file listings. Good luck with the coding!
__________________
my own tutorial website... i love it... yeah i really do. marry me ..

http://www.tutorials-expert.com
Posted:  10 Nov 2006 14:13
Thats a great tutorial!

I think i need to fix the code css though...that green and black thing is just not working

But neat tutorial. Very well explained. Good job
__________________
http://www.vipercreations.com/images/banner/result.gif
Posted:  10 Nov 2006 17:01
Hey, haxan,

why don't you enter this into Pixel2life or something.
I'm sure they would probably accept it.

I don't know a lot about PHP but if danielneri says its okay, i guess its worth attempting...

Check it out.. and let us know.
__________________
http://i42.photobucket.com/albums/e336/arunkurian92/halocopy.gif
Posted:  11 Nov 2006 15:31
cool,

i will try.

thanks though. 
__________________
my own tutorial website... i love it... yeah i really do. marry me ..

http://www.tutorials-expert.com