...

View Full Version : How do I check the total number of files in a folder?



MrCat
06-18-2006, 02:20 AM
I want to use a loop to list the file names in a folder. How do I find out the total number of files in the folder so I know when to end the loop? I can't use 'file exists' because the file names could be anything.

Nicklas
06-18-2006, 02:40 AM
If you use PHP5, then you can do something like this

<?php

$file = scandir("path/to/your/folder");

foreach($file as $key => $value) {

if(is_file($value)) {
$total++; // Counter
echo "$value<br />\n";
}

}

echo "<p>Total files: $total</p>";

?>


If you dont use PHP5, then do something like this

<?php

$dir = "path/to/your/folder";

if ($check = opendir($dir)) {

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

if (is_file($file)) {
$total++; // Counter
echo "$file<br />\n";
}

}

}

echo "<p>Total files: $total</p>";

?>

jspeybro
06-18-2006, 12:15 PM
I use something like this:


$files = array();
$dir = opendir('./smily');
while(($file = readdir($dir)) !== false)
{
if($file !== '.' && $file !== '..' && !is_dir($file))
{
$files[] = $file;
}
}
closedir($dir);
sort($files);

requesting the lenght of the array will get you the number of files.
and to go through all these files:
[code]
for($j=0; $j<(count($files)-1); $j++)
{
echo ($files[$j]);
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum