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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.