<?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
<?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
<?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);
?>