Directory Listings, list files and subfolder using php

Sunday, 27 April 2008 00:00 Chetankumar Akarte
Print
Directory Listings, list files and subfolder using php

This is the simple example to list contents of any Directory. To do this we are going to define function DirDisply( ) which will read the current directory contents and display it as a list.

We first open current directory by $TrackDir=opendir(".");
The opendir function returns a directory handle resource on success, or FALSE on failure.

Syntax is:
opendir ( string path [, resource context] )

Where:
Path - The directory path that is to be opened

Context - For a description of the context parameter, refer to the streams section of the php manual.

If path is not a valid directory or the directory cannot be opened due to permission restrictions or file system errors, opendir() returns FALSE and generates a PHP error of level E_WARNING. You can suppress the error output of opendir() by prefix'@' to the front of the function name.

Here “.” Indicates the directory which contain current php file and read each element of directory and display it using a while loop.


while ($file = readdir($TrackDir)) {

if ($file == "." || $file == "..") { }
else {
print "<tr><td><font face=\"Verdana, Arial, Helvetica, sans-serif\"><a href=$file target=_blank>$file</a></font> </td>";
print "<td>  ".filetype($file)."</td></tr><br>";

}

}


And then we have to close directory by closedir($TrackDir); Which Closes the directory stream indicated by dir_handle. The stream must have previously been opened by opendir().


Syntx:
void closedir ( resource dir_handle )

Where,
dir_handle - The directory handle resource previously opened with opendir().

A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions.

When I run complete script on Web server I got result as below shown in figure:
Directory Listings

Our complete piece of code will be as listing below:

<?php
/*
function that reads directory content and
returns the result as links to every file in the directory
also it disply type wheather its a file or directory
*/
function DirDisply() {

$TrackDir=opendir(".");

while ($file = readdir($TrackDir)) {

if ($file == "." || $file == "..") { }
else {
print "<tr><td><font face=\"Verdana, Arial, Helvetica, sans-serif\"><a href=$file target=_blank>$file</a></font> </td>";
print "<td>  ".filetype($file)."</td></tr><br>";

}
}
closedir($TrackDir);
return;
}

?> <b><font face="Verdana, Arial, Helvetica, sans-serif">Current Directory Contain
Following files and Sub Directories...</font></b>
<p>
<?php
@ DirDisply();
?>


Download
Download source code for 'Directory Listings, list files and subfolder using php'

Download
Trackback(0)
Comments (3)Add Comment
...
written by suresh, November 22, 2008
Excellent programme but it is better to add upload file and view the contents dynamically
Changed the html to be correct
written by andy, December 18, 2008
This will make the html aspect of this code correct and html compliant (target=_blank is not xhtml, would need additional javascript to complete this aspect, but then who wants to open a new page from a local directory??). still maybe it would be better as a list then there would be fewer elements. For formatting, just add a class to the table and format with CSS as normal. no-inline styles needed.

Also removed htaccess and ds_store from the listing also, as 99% of people do not want to browser them.

function DirDisply() {

$TrackDir=opendir(".");

while (false !== ($file = readdir($TrackDir))) {
print "";
if ($file == "." || $file == ".." || $file == ".htaccess" || $file == ".DS_Store") { }
else {
print "$file";
print " ".filetype($file)."";
}
print "";
ignore temp files from MS-Office and only show DOC PDF XLS
written by Peter Geier, March 18, 2009
Here is another variant which ignores temp files starting with ~$ and only shows specific documents:

function DirDisply() {

$TrackDir=opendir("your_directory");
$RealDir="your_directory";
$IgnoreFile="~$";

while ($file = readdir($TrackDir)) {

if (strpos($file, '.doc')>0 || strpos($file, '.pdf')>0 || strpos($file, '.xls')>0)
{
if (substr($file,0,2)==$IgnoreFile) { } else
{
print "";
print "$file ";
print " ".filetype($file)."";
} }
else { }
}
closedir($TrackDir);
return;
}

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
Last Updated ( Thursday, 14 May 2009 21:52 )