![]() |
please help me to count the number of folders with in directories
Hi All,
I am listing all the 'categories' with in a directory which is working now i want to try and find out the number of "folders" with in each of the category directories here is my code PHP Code:
can anyone help me please thanks Luke |
Do you need to count the subdirectories of the $directory, or the subdirectories of the $entry?
|
of the $directory :)
|
I'd write a filter function and then use a scandir:
PHP Code:
|
ok thanks for the above,
i have added the function to my page and added these two lines in my while loop PHP Code:
PHP Code:
am i using it correctly? maybe my loop is incorrect? cheers mate Luke |
Another solution, once you have a folder path... this will count the number of folders immediately within that folder (non-recursive):
PHP Code:
|
Sorry you'll need to verify its a directory before as well.
PHP Code:
I'm betting you do not have the function, unless you've modified it. I'd have a syntax error on this line: if (is_dir($itm) && !in_array($itm, array('.', '..')) since I'm missing a closing ).Edit: Oh yeah I forgot that glob has an ONLYDIR directive! Wait, does that include the '.' and '..' as well? |
i changed the function and added the missing ')' :)
ok i did some debugging and print_r($directories) echos out the directory paths. print_r($aItems) echos out the following: Code:
Array ( [0] => . [1] => .. [2] => New Text Document.txt [3] => New folder ) Array ( [0] => . [1] => .. ) Array ( [0] => . [1] => .. ) Array ( [0] => . [1] => .. ) Array ( [0] => . [1] => .. ) Array ( [0] => . [1] => .. )print_r($aItemsFiltered) echos out: Code:
Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )how does one make sure that the filterArrayOnlyDirectories function is within scope? cheers |
Quote:
|
thanks kbluhm your solution works :)
would still like to get your method working fou-lu always nice to see different approaches to a problem thanks guys |
Nice. I've been using iterators for so long I've forgotten about some of these more basic ones. And the globiterator itself let .'s into it depending on the flags.
To make sure its within scope, you simply add it to this page to be accessible or to a file that is included into this one. Sounds like its not in scope though since the original code would have thrown a fatal error since it would have had a syntactical error. Functions are global so long as they are not nested and are not a part of a class or namespace. Conditional branches also dictate if functions are available, so don't put it in an if condition. |
thanks guys one more question if i may :)
if Code:
$sub_directories = glob( realpath( $directory ) . '/*', GLOB_ONLYDIR );many thanks Luke |
There is no corresponding GLOB_ONLYFILE directive. However, so long as you have named extensions, then you can simply use a glob pattern of
'/*.*'. If you don't have extensions, you'll need to make use of !is_dir.Alternatively, you can pull a glob() of all records, and a glob of GLOB_ONLYDIR. Then you can use array_diff to remove the ones from the only directories glob from the entire glob which will leave you with only the files. That should be doable as such (I can't test this where I am I'm afraid): PHP Code:
|
excellent thank you very much!!!
|
Yet another alternative for your files vs. folders inquiry:
PHP Code:
|
| All times are GMT +1. The time now is 01:57 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.