Showing posts with label directory listing. Show all posts
Showing posts with label directory listing. Show all posts

Thursday, March 3, 2011

Reading directory content

Hi,

If you are not sure about the content of a directory, this is how you can get it. This is helpful in scenarios e.g.  where the admin or any other user uploads music files, images etc to a specific directory in the server via some FTP client(e.g. filezilla) instead of a conventional web page with file uploader.

To achieve this you just need 2 main functions, opendir() and readdir(). Here is how I had done it:
<?php
$dir_path="repository/files/";
$browse_directory = opendir($dir_path);
while($entries = readdir($browse_directory)) {
$dir_array[] = $entries;
}
closedir($browse_directory);
$index_cnt= count($dir_array);
Print ($index_cnt." files<br>\n");

sort($dir_array);
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Type</th><th>File size</th></TR>\n");
for($index=0; $index < $index_cnt; $index++) {
print("<TR><TD><a href=\"$dir_array[$index]\">$dir_array[$index]</a></td>");
print("<td>");
print(filetype($dir_path.$dir_array[$index]));
print("</td>");
print("<td>");
print(filesize($dir_path.$dir_array[$index]));
print("</td>");
print("</TR>\n");
}
print("</TABLE>\n");
?>
The output of the above will be something like: