Tuesday, January 11, 2011

PHP list directory and files permission

SkyHi @ Tuesday, January 11, 2011
<?php
$dir = '/var/www/html/test3/';
$dirperm = substr(sprintf('%o', fileperms("$dir")), -4);
if ($handle = opendir("$dir")) {
    echo "Directory handle: " . '<br / >';
    echo $dirperm . ' ' .  $dir . '<br / >';
    echo "Files:" . '<br / >';

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                        $filelink = "<a href=\"$file\">$file</a>";
                        echo substr(sprintf('%o', fileperms("$file")), -4). ' ' . $filelink . '<br / >';
                }
    }
closedir($handle);
}

?>


OUTPUT:

Directory handle:
2755 /var/www/html/test3/
Files:
0644 perm2.php
0644 getelementby4.html
0644 getelementbytag.html

REFERENCES
http://php.net/manual/en/function.readdir.php
http://php.net/manual/en/function.fileperms.php