PHP scandir() Examples


scandir() function scan the whole directory and list all files and subdirectories.

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

To get all files in a directory with certain pattern, use glob() function:
<?PHP
    $dir = "/usr/";
	$arrfiles = glob('*.txt');
	var_dump($arrfiles);
?>