Jump to content

[SOLVED] functions for num subdirectories and files in a directory?


phpknight

Recommended Posts

Are there php functions that get the number of subdirectories or files in a given directory?  I cannot locate one.

 

If so, is it fast?  I plan on having a lot of files/directories, so I just want a number, not a list.  The server people tell me that only approx. 32000 subdirectories can be in any given directory, so I want to switch directories when it starts getting near that limit.

 

This is thinking way ahead, but I would rather plan for it now than encounter problems later.

 

 

Basically files and folder in a directory could be counted like:

$path = 'somedir';
$dir = opendir($dir);
$counter = 0;
while (($file = readdir($dir)) !== false) {
     $counter++;
}
echo $counter;

 

U will need to make a recursive function which if finds a directory with is_dir(), starts itself over. Im currently thinking of creating a file manager and maybe ill try creating such an option which counts the total files and subdirectories of a directory. If ill have smth before you get this done, ill write it.

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

i think no need to condition for flase

while ($file = readdir($dir)) {

is ok

 

why

$file = readdir($dir) true if variable file get the value from  readdir($dir) and false if not

Once again, thanks for the input.  Once difficulty with these options is that it still has to go through a loop.  And if a directory has 10000 directories and 1 million files, that would be a time drain.  I was hoping that PHP just had a function that called the system and got some number.

At least that function is better.  I was looking in the file functions.  Thanks for the page.  It still takes up a lot of memory with that array, though.  I am surprised there is just not something simple that returns a number and doesn't have to loop.  I thought operating systems would have something like that.

scandir() is for PHP5, so on a shared host which runs PHP4 u cant do much. Anyway u could try using system commands with exec(). U could use exec('tree', $arr) and count those lines with count($arr). Dont know if it is faster or not, but it can be worth the try.

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.