Directory Listings, list files and subfolder using php

E-mail Print PDF
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
Comments (0)Add Comment

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 ( Sunday, 27 April 2008 18:25 )  

Who's Online

We have 4 guests online

Newsflash 4

Yesterday all servers in the U.S. went out on strike in a bid to get more RAM and better CPUs. A spokes person said that the need for better RAM was due to some fool increasing the front-side bus speed. In future, buses will be told to slow down in residential motherboards.



Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo

Special features

Canon PowerShot S3 IS Canon PowerShot S3 IS Ut massa risus a vitae lacus wisi laoreet id nibh mus. Enim quis hac interdum aliquam tortor lorem nulla leo enim vita...
 
HTC touch HTC touch Elit at ridiculus massa odio Pellentesque auctor at justo eget Vivamus. Rhoncus tincidunt nisl mauris enim tortor ip...
 
Nintendo Wii Nintendo Wii Lorem ipsum dolor sit amet consectetuer et sed orci iaculis aliquet. Justo Praesent lacinia at ac tellus Curabitur e...
 



Apple

Apple TV

Lorem ipsum dolor sit amet consectetuer id nibh convallis suscipit netus

iTunes Gifting

iPod + iTunes Gifting

Quisque elit dolor ut congue sed ligula turpis Phasellus Aenean hac. Et Sed ipsum

Apple TV

Apple TV

Lorem ipsum dolor sit amet consectetuer id nibh convallis suscipit netus

iPod + iTunes Gifting

iPod + iTunes Gifting

Quisque elit dolor ut congue sed ligula turpis Phasellus Aenean hac. Et Sed ipsum

iPod Your Life

iPod Your Life

Tortor Aenean tincidunt pede massa Nunc id dui massa elit In. Nam amet a tellus lacus