Page 1 of 1

File Ops: An array containing file names within a directory

Posted: Sun Oct 13, 2002 9:09 pm
by dickey
I have looked at the PHP file system commands, and I cannot see a function that would return an array of file names contained in a target directory.

A list of filenames i.e. ls on unix, or dir on windows.

Any advice would be greatly appreciated.

Thank you

- Dickey

Posted: Sun Oct 13, 2002 9:17 pm
by volka
http://www.php.net/manual/en/function.readdir.php
not returning an array of filenames but you will be able to create one.

also note http://www.php.net/manual/en/function.is-dir.php

Posted: Mon Oct 14, 2002 1:30 am
by rev
I'm feeling nice... :)

Per example 2 on http://www.php.net/manual/en/function.readdir.php this function will create an associative array of files in alphabetical order from a given directory.

Code: Select all

<?php
function getFileList($theDIR) {
	$fileArray = array();
	if($handle = opendir($theDIR)) {
		while(false !== ($file = readdir($handle))) {
			if($file != "." && $file != "..") {
				if(@is_file($theDIR . "/" . $file)) {
					$fileArrayї$file] = $theDIR . "/" . $file;
				}
			}
		}
		closedir($handle);
	}
	asort($fileArray);
	reset($fileArray);
	return $fileArray;
}

$fileListArray = getFileList("/path/to/dir");
?>