List files and directories

来源:互联网 发布:java \\d是什么 编辑:程序博客网 时间:2024/05/29 10:31

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.

<script type="text/javascript"><!--google_ad_client = "pub-6953732755193150";google_alternate_ad_url = "http://www.999tutorials.com/google_adsense_script.html";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text";google_ad_channel ="4126591546";google_color_border = "FFFFFF";google_color_bg = "FFFFFF";google_color_link = "DE3866";google_color_url = "000000";google_color_text = "000000";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

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.

<?

// 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!
 

原创粉丝点击