Advertisements

list_files

Definition:
function list_files( $folder = '', $levels = 100 ) {}

Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.
The depth of the recursiveness can be controlled by the $levels param.

Parameters

  • string $folder: Full path to folder
  • int $levels: (optional) Levels of folders to follow, Default: 100 (PHP Loop limit).

Return values

returns:False on failure, Else array of files

Source code

function list_files( $folder = '', $levels = 100 ) {

	if ( empty($folder) )

		return false;



	if ( ! $levels )

		return false;



	$files = array();

	if ( $dir = @opendir( $folder ) ) {

		while (($file = readdir( $dir ) ) !== false ) {

			if ( in_array($file, array('.', '..') ) )

				continue;

			if ( is_dir( $folder . '/' . $file ) ) {

				$files2 = list_files( $folder . '/' . $file, $levels - 1);

				if ( $files2 )

					$files = array_merge($files, $files2 );

				else

					$files[] = $folder . '/' . $file . '/';

			} else {

				$files[] = $folder . '/' . $file;

			}

		}

	}

	@closedir( $dir );

	return $files;

}

2271

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: