Jump to content

File and Directory Lister. Not working (Fully)


AbydosGater

Recommended Posts

Hey guys. Im working on a directory and file lister.. that will obviously display all files and directorys within a given path.

The code i am using is as follows:

<?php
set_time_limit(0);
function scan_dir($DIR){
	global $dirs;
	$dp = opendir($DIR);
	$dirs = array();
while (false !== ($file = readdir($dp))){
	if (is_dir($DIR .$file)){
		if($file !== '.' && $file !== '..'){
			$dirs[] = $DIR.$file.'/';
			echo $DIR.$file.'/'.'<br />';
		}
	}
}
}
scan_dir("path/to/scan/");
while ($dirs){
foreach ($dirs as $dir){
	scan_dir($dir);	
}
}
?>

 

Now. I tried it within the folder its in (dir_reader) and within there i created some other folders and sub folders..

The output was as follows:

c:/wamp/www/dir_reader/IM A DIR/
c:/wamp/www/dir_reader/IM A DIR/ME2/
c:/wamp/www/dir_reader/IM A DIR/SO AM I/
c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/
c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/ME4/
c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/ME4/ME5/
c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/ME4/ME5/ME6/

So as you can see i would expect it to be working completely.

But if i run it on my full /www/ dir which has maybe 30 folders some with maybe 30 subfolders and it goes on forever lol .. so i want to list everything. But when i do. It doesnt list everything...

 

It lists quite a few folders really.. But nowhere near as many as there is to be listed.

 

Can anyone see what it stops listing directorys? Its nothing to do with time limits cause my max_execution_time is 30 secs but, this only take less then 1 to load.

 

Anyone see anything wrong?

 

Andy

You need to use recursion...

 

Here is an example of what you are wanting to do:

 

http://us.php.net/manual/en/function.readdir.php#75156

 

Notice how when the script is going through the files in the directory it's looking through, if it sees that the file is actually a directory it calls itself to parse that directory.

You might find this useful. I made this for another topic somwhere:

 

<?php
function no_of_files($start_dir,$sub_dir=FALSE){
	$no_of_files=0;
	if($sub_dir === false){
	$handler = opendir($start_dir);	
}else{
 	$start_dir = $start_dir.$sub_dir;
	$handler = opendir($start_dir);
}

while(false !== ($file = readdir($handler))){
	if($file != '.' && $file != '..'){
		if(is_dir($start_dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined
			$dir_name = '\\'.$file;
			echo $dir_name.'<br />';
			$no_of_files += no_of_files($start_dir,$dir_name);	
		}else{//is a file, so increase our counter
			$no_of_files++;	
		}	
	}
}
return $no_of_files;
}
echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music');
?>

 

This was to return the number of files(not folders) in a given directory and all its sub directories. Could be modified to echo names and include folders etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.