PHP/File Directory/scandir
Listing All Files in the Current Directory
<?php
function numfilesindir ($thedir){
if (is_dir ($thedir)){
$scanarray = scandir ($thedir);
for ($i = 0; $i < count ($scanarray); $i++){
if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
if (is_file ($thedir . "/" . $scanarray[$i])){
echo $scanarray[$i] . "<br />";
}
}
}
} else {
echo "Sorry, this directory does not exist.";
}
}
echo numfilesindir ("sample1");
?>
scandir.php
<?php
print_r(scandir("/usr/local/apache2/htdocs"));
?>
The scandir( ) function takes a minimum of one parameter with an optional second.
<?
$files = scandir(".", 1);
var_dump($files);
?>