PHP scandir()

scandir() function scans the whole directory and list all files and subdirectories. It returns an array of all filenames, or FALSE if failed.
   array scandir(str dir [, int order = SCANDIR_SORT_ASCENDING, resource context]);

<?PHP
	$dir = "/usr/";
	foreach (scandir($dir) as $f) 
	{
	  if ($f !== '.' and $f !== '..')
	  {
		  echo "$f\n";
	  }
	}
?>

There are three sorting parameters:
   SCANDIR_SORT_ASCENDING,
   SCANDIR_SORT_DESCENDING,
   SCANDIR_SORT_NONE.

To get all files in a directory with certain pattern, use glob() function:

<?PHP
    $dir = "/usr/";
	$arrfiles = glob('*.txt');
	var_dump($arrfiles);
?>

The following code can list all files in a specified directory.

<?PHP
	function allfiles($dir, $prefix = '')
	{
		$dir = rtrim($dir, '\\/');
		$result = array();
		foreach (scandir($dir) as $f) {
		  if ($f !== '.' and $f !== '..') {
			if (is_dir("$dir\\$f")) {
			  $result = array_merge($result, allfiles("$dir\\$f", "$prefix$f\\"));
			} else {
			  $result[] = $prefix.$f;
			}
		  }
		}
		return $result;
	}
?>

By default, all the files returned by scandir() will be sorted ascending. Following code can sort all the files by the date modified.

<?PHP
	function allfiles($dir, $prefix = ''){...}  //See above

	$dir = "used/";
	$files = allfiles($dir);
	$times = array();
	foreach ($files as $file)
	{
		$times[$file] = filemtime($dir.$file);
	}
	arsort($times); //from most recently modified to ...
	//asort($times); //reverse
	$files = array_keys($times);
?>

Following code can sort all the files by file size.

<?PHP
	function allfiles($dir, $prefix = ''){...}  //See above

	$dir = "used/";
	$files = allfiles($dir);
	$times = array();
	foreach ($files as $file)
	{
		$times[$file] = filesize($dir.$file);
	}
	arsort($times); //from largest size to smallest size
	//asort($times); //reverse
	$files = array_keys($times);
?>


:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home