Page 1 of 2

[SOLVED] - return # of files in multiple folders

Posted: Thu Jun 23, 2005 8:20 am
by phpnewbdude
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)

Code: Select all

<?php
//get path of directory containing this script
$dir = $_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF); 
//open a handle to the directory
$handle = opendir($dir);
//intitialize our counter
$count = -1;
//loop through the directory
while (false !== ($file = readdir($handle))) {
//evaluate each entry, removing the . & .. entries
if (is_file($file) && $file !== '.' && $file !== '..') {
$count++;
}
}
echo $count;
?> Pictures

Posted: Thu Jun 23, 2005 8:47 am
by timvw
All you should do is add a switch, test if is_dir($path . '/' . $file) and recurse ...

Or you could make use of the "ls" (options -laR) and pipe it to "wc"
$result = `ls -lR $directory | wc -l`;

Posted: Thu Jun 23, 2005 8:53 am
by CoderGoblin
The way I would suggest is using recursion, something like...

Code: Select all

<?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.

Posted: Thu Jun 23, 2005 9:17 am
by phpnewbdude
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?

your script in action:
http://www.ozzyhead.com/goop/goop_galle ... ounter.php

Posted: Thu Jun 23, 2005 9:21 am
by phpnewbdude
Should have noted that line 14 is:

} elseif (is_dir($file) }

Posted: Thu Jun 23, 2005 9:26 am
by CoderGoblin
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.

Posted: Thu Jun 23, 2005 10:50 am
by phpnewbdude
Coder, I'm getting a parse error with this...

Parse error: parse error, unexpected $ in /home/ozzyhead/www/goop/goop_gallery/maincounter.php on line 22

which is the very last line of code... any idea?

Tim, I also attempted your version, but I was unable to get it to work...

Posted: Fri Jun 24, 2005 7:09 am
by phpnewbdude
Can anyone help me with the code above?

Posted: Fri Jun 24, 2005 7:10 am
by John Cartwright
change

Code: Select all

return count;
to

Code: Select all

return $count;

Posted: Fri Jun 24, 2005 7:16 am
by phpnewbdude
Something is still wrong with the code... I am getting an unexepected $ parse error in line 23 which is ?>

Any clues?

Posted: Fri Jun 24, 2005 7:19 am
by John Cartwright
Re-post your code please.

Posted: Fri Jun 24, 2005 7:20 am
by phpnewbdude

Code: Select all

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

?>

Posted: Fri Jun 24, 2005 7:27 am
by wwwapu
Look at line 19

Code: Select all

$_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF) // $PHP_SELF ?
$_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF'])

Posted: Fri Jun 24, 2005 7:29 am
by John Cartwright
Also count your curly brackets

Posted: Fri Jun 24, 2005 7:33 am
by phpnewbdude
I didn't write that script... i really don't know what I'm doing. I am brand new to php. Can someone fix this code for me? What's wrong with 19?