Page 1 of 1

counting files in directories

Posted: Wed Jun 14, 2006 6:35 pm
by Luke
I know I can just read all the files in the directory with readdir() but is there a more effective way to do this if i just wish to know how many files are inside of a directory?

Posted: Wed Jun 14, 2006 7:03 pm
by feyd
my favorite, but slightly complicated for some

Code: Select all

$num = count(array_filter(glob('/some/path/*'),'is_file')));

Posted: Wed Jun 14, 2006 8:21 pm
by defect
not sure about effectiveness but i like this way:

Code: Select all

$num = shell_exec("ls /path/to/dir|wc -l");

Posted: Wed Jun 14, 2006 9:41 pm
by Ambush Commander
Using shell commands is highly unrecommended. Feyd's method is quite quick. If you really wanted things to work out nice, you'd define global_countFilesInDir($directory) function and implement it there, so that you could change it if something better came along.

Posted: Thu Jun 15, 2006 10:10 am
by Luke
I've never even used the shell_exec function. That is very interesting... I will look into that (not for this, but just because it's interesting).

Thanks feyd... that worked very nicely.