Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2006, 01:20 AM   PM User | #1
MrCat
New Coder

 
Join Date: Jun 2006
Location: New Zealand
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
MrCat is an unknown quantity at this point
How do I check the total number of files in a folder?

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.
MrCat is offline   Reply With Quote
Old 06-18-2006, 01:40 AM   PM User | #2
Nicklas
New Coder

 
Join Date: Jun 2006
Location: Sweden
Posts: 49
Thanks: 0
Thanked 3 Times in 3 Posts
Nicklas is on a distinguished road
If you use PHP5, then you can do something like this
PHP Code:
<?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 Code:
<?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>";

?>
Nicklas is offline   Reply With Quote
Old 06-18-2006, 11:15 AM   PM User | #3
jspeybro
New Coder

 
Join Date: May 2004
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
jspeybro is an unknown quantity at this point
I use something like this:

Code:
$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]);
}
?>
jspeybro is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:35 AM.


Advertisement
Log in to turn off these ads.