Page 1 of 1

Directory browsing code???

Posted: Sat Oct 08, 2005 9:03 pm
by alex.barylski
Have the following function(s) which I borrowed from the PHP web site:

Code: Select all

function list_dirs($path_name='.')
	{
 		$arr_dirs = array();

 		//
 		// Borrowed from the PHP web site
		if($handle = opendir($path_name)){
			while(false !== ($dir = readdir($handle))){
				if(is_dir($dir)){
					if($dir != '.' && $dir != '..')
						$arr_dirs[] = $dir;
				}
			}

			closedir($handle);
			return $arr_dirs;
		}

		return false; // Error occurred :(
	}

	function list_files($path_name='.')
	{
 		$arr_dirs = array();

 		//
 		// Borrowed from the PHP web site
		if($handle = opendir($path_name)){
			while(false !== ($dir = readdir($handle))){
				if(is_file($dir)){
					$arr_dirs[] = $dir;
				}
			}

			closedir($handle);
			return $arr_dirs;
		}

		return false; // Error occurred :(
	}
I am calling these functions and sometimes getting errorenous(sp) results...

Only it seems, when the directory passed to scan is that of the current working directory (either implicitly by scanning directory which currently executing script is in or by calling chdir() ) does the function return all results properly???

Anyone have alternative code which returns an array???

Please it's important these 2 functions stay as 2 and do not amalgamate - although technically the same results would be yielded...I need the functions kept as two :)

Anyone have any clue???

This code is driving me nutts

I'm running it on a shared host, running php 4.3.10 using Linux.

Thanks in advance :)

Posted: Sat Oct 08, 2005 9:12 pm
by feyd
readdir() returns only the name of the active file/directory in the opened directory. You must concatenate the the directory name you opened with the name you receive from readdir()

Posted: Sat Oct 08, 2005 9:28 pm
by alex.barylski
feyd wrote:readdir() returns only the name of the active file/directory in the opened directory. You must concatenate the the directory name you opened with the name you receive from readdir()
In other words, what your saying is everytime I change directories I would have to do what you said or chdir() and then readdir() would list everything properly???

I think I actually tried what you said...but not what I suggested...

It would occassionally return semi-results...

Like one or two of the files or folders and then stop...

Regardless I switched my code to using glob() which seems to be working as expected...without how many lines of code :)

So i'm happy :)

Thanks for you helpful insight...i'll keep that in mind for future problems :)

Cheers :)