PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi, I've been using the following code to count pictures that I have on my website in each individual folder. Is there a way to modify this script, or is there another script which will count all of the pictures i have in all of the folders? Looking for a grand total of all combined folders. (see http://www.ozzyhead.com/pictures.htm for current use of this script)
<?php
function getPictures($dir)
{
//open a handle to the directory
$handle = opendir($dir);
//loop through the directory
while (false !== ($file = readdir($handle))) {
//evaluate each entry, removing the . & .. entries
if (is_file($file) && $file !== '.' && $file !== '..') {
$count++;
} elseif (is_dir($file)) {
$count=$count+getPictures("$dir/$file");
}
}
return count;
}
$count=getPictures($_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF));
echo $count;
?>
Untested but should give you a starting point. You should also include some method to break out if you get in a loop. Things like shortcuts/symbolic links may cause an infinate loop if you are not careful. The php command is_link may be of use. I would always also include the use of the static variable to leave if too many repetitions occur (useful safety net for any recursion).
EDITED: Correction to SCRIPT.
Last edited by CoderGoblin on Thu Jun 23, 2005 9:30 am, edited 5 times in total.
Codergoblin... i tried your code. I'm very new to php, so I'm assuming i would upload your code right into the main directory which holds the folders of which I want the files to be counted?
I tried it, and got an unexpected } in line 14. When I take it out, i get an unexpect "T" error. I really don't have a clue what I'm doing here... lucky I got the first script to work so well... can you help?
Sorry missed a closing bracket. Corrected on the script rather than repeated here. Suugest you copy the code again as I also removed a redundant line that may have caused problems.