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. :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.