Get A List of Files or Directories

PHP Code

function Files_FromDir ($Dir, $DirSep = null) {
	/*
	Purpose
		Used to get a list of files from the specified directory.
	Parameters
		Dir (string) = The full path to the directory we're using.
		DirSep (optional string) = The directory separator used in the specified directory (Dir).
			Default if null: DIRECTORY_SEPARATOR
			Default: null
	Return (string array)
		Returns a list of all files in the specified directory.
		Full file paths are returned.
		Only files are returned. Folders are skipped.
		Returns false if:
			- The specified directory (Dir) is invalid or inaccessible.
	Version History
		1.0	2015.05.14 Tested by Russ Tanner on PHP 5.6.8 Win 7-64.
	*/

	// Default the directory separator.
	if (is_null($DirSep)) $DirSep = DIRECTORY_SEPARATOR;

	// Open the specified directory.
	$hFile = @opendir($Dir);
	// Error Trap: The specified directory is invalid. Return false and exit now.
	if ($hFile === false) return false;

	// Loop while files are available.
	while (false !== ($FileName = readdir($hFile))) {
		// Do not process "." or ".."
		if ($FileName == '.' || $FileName == '..') continue;
		// Only process files. Skip directories.
		if (!is_file($Dir . $DirSep . $FileName)) continue;
		// This is a valid file. Append the full path to the return buffer.
		$r[] = $Dir . $DirSep . $FileName;
	}
	
	return $r;

}
 



PHP Code

function Dirs_FromDir ($Dir, $DirSep = null) {
	/*
	Purpose
		Used to get a list of directories from the specified directory.
	Parameters
		Dir (string) = The full path to the directory we're using.
		DirSep (optional string) = The directory separator used in the specified directory (Dir).
			Default if null: DIRECTORY_SEPARATOR
			Default: null
	Return (string array)
		Returns a list of all directories in the specified directory.
		Full directory paths are returned.
		Only directories are returned. Files are skipped.
		Returns false if:
			- The specified directory (Dir) is invalid or inaccessible.
	Version History
		1.0 2016.08.07 Tested by Russ Tanner on PHP 5.6.1.
	*/

	// Default the directory separator.
	if (is_null($DirSep)) $DirSep = DIRECTORY_SEPARATOR;

	// Open the specified directory.
	$hFile = @opendir($Dir);
	// Error Trap: The specified directory is invalid. Return false and exit now.
	if ($hFile === false) return false;

	// Loop while files are available.
	while (false !== ($FileName = readdir($hFile))) {
		// Do not process "." or ".."
		if ($FileName == '.' || $FileName == '..') continue;
		// Only process directories. Skip files.
		if (!is_dir($Dir . $DirSep . $FileName)) continue;
		// This is a valid directory. Append the full path to the return buffer.
		$r[] = $Dir . $DirSep . $FileName;
	}
	
	return $r;

}
 

Last edited by Russ; 08/07/16 01:50 AM.

The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)