Jump to content

Counting files in a directory?


Vivid Lust

Recommended Posts

It deosnt work ???

 

<?php

$count = count(glob("http://imgsurf.freehostia.com/uploads/*")); # keep the asterick.

echo $count;

?>

 

http://imgsurf.freehostia.com/count.php

I was going to say you'll need your full path, which you can find by placing a file in your "uploads" folder called, lets say for instance, testblah.php with the contents:

<?php
prin/*t "hi";
?>

That should give an error, the path of the error that it gives is your full path, minus the filename of the file. :)

If you wanted to count the number of files in a particular folder and all sub folders, you'd need to use recursion. Here's a blue peter-esque one i made earlier :P

 

<?php
function no_of_files($dir){
    $no_of_files=0;
    $handler = opendir($dir);

    while(false !== ($file = readdir($handler))){
        if($file != '.' && $file != '..'){
            if(is_dir($dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined
                $sub_dir = $dir.'\\'.$file;
                $no_of_files += no_of_files($sub_dir);	
            }else{//is a file, so increase our counter
                $no_of_files++;	
            }	
        }
    }
    return $no_of_files;
}
echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.