Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/templates/ja_barite/ja_menus/Base.class.php on line 62
Directory Listings, list files and subfolder using php

Excellent Fundamental...!!!

 
  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • green color
  • blue color

Deprecated: Assigning the return value of new by reference is deprecated in /home/content/48/9096548/html/xfunda.com/components/com_jomcomment/bbcode.php on line 172

Deprecated: Assigning the return value of new by reference is deprecated in /home/content/48/9096548/html/xfunda.com/components/com_jomcomment/bbcode.php on line 956

Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/plugins/content/emailcloak.php on line 50

Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/plugins/content/emailcloak.php on line 67

Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/plugins/content/emailcloak.php on line 79

Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/plugins/content/emailcloak.php on line 99

Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/plugins/content/emailcloak.php on line 112

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
Trackback(0)
Comments (4)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;
}
Sort?
written by SonshineTN, October 03, 2010
I have a directory full of numbered photos. When I run this code, they are listed but all out of order. Photo034.jpg is first, followed by Photo025.jpg, next is Photo035.jpg... I would have thought it would list the files as they appear in the directory. All were created in sequential order as well so it's not sorting on creation date. How does it pull the files? Can I add code to sort the directory? Any help would be appreciated. Thanks!

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 )  

Articles


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Adobe Gallery


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Community


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

FAQ's


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Resources


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Who's Online

We have 22 guests online

Subscribe Feeds

Enter your email address:

Sponsor Adds


 
 

Submit Article

We want to develop such a Community portal which will provide a good resource on latest hot technologies such as .NET, VB.NET, C#.NET, ADO.NET, ASP.NET, PHP, Flash, Photoshop. If you like to write for us, this guideline for you. please send your article, faqs on admin[at]xfunda.com

Adobe Gallery Photoshop Tutorials...

Photoshop Tutorial - Create Colorful Composition in Photoshop Photoshop Tutorial - Create Colorful Composition in Photoshop Photoshop Tutorial - Create Colorful Composition in Photoshop Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Category - Photoshop >> Photo Effects. ...
 
Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Category - Photoshop >> Photo Effects. ...