?
Solved

php to list file in directory and sub directory

Posted on 2009-02-08
3
Medium Priority
?
1,115 Views
Last Modified: 2012-05-06
How can i write code to list files in directory and subdirectory with php
0
Comment
Question by:teera
3 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 23587382
0
 
LVL 2

Accepted Solution

by:
thomas_nc earned 2000 total points
ID: 23587595
You can write a function that reads a directory and call it recursively for for any items that are subdirectories.

A sample function is attached.

<?php
function dirlist( $dir )
{
	$dh = opendir( $dir );
	if( $dh )
	{
		while( $subitem = readdir($dh) )
		{
			// Ignore . and ..
			if( preg_match('/^\.\.?$/', $subitem ) ) continue;
 
			echo "$dir/$subitem\n";
 
			// Call  function recursively if $subitem is a directory
			if( is_dir("$dir/$subitem") )
				dirlist("$dir/$subitem");
		}
		closedir($dh);
	}
	else
		die("Directory '$dir' not found.");
 
}
 
?>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question