[SOLVED] - return # of files in multiple folders

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!

Moderator: General Moderators

User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

There is no such thing as $PHP_SELF. It doesn't exist. You can access your PHP_SELF (eg. current script) with $_SERVER['PHP_SELF'].

Would this be a great place to inform about existence of The Manual at http://www.php.net

And sorry I looked at the first post by codergoblin.
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

I'm really at a loss here. I modified the script to this... but now I'm getting parse error, unexpected T_ECHO in line 21. I just want to get this script working. It's driving me mad...

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

$_SERVER['DOCUMENT_ROOT'].dirname($_SERVER)
echo $count;

?>
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Code: Select all

<?php 
function getPictures($dir)
{ //funktion begin  
//open a handle to the directory 
$handle = opendir($dir);  
//loop through the directory  
while (false !== ($file = readdir($handle))) { //while begin
//evaluate each entry, removing the . & .. entries
if (is_file($file) && $file !== '.' && $file !== '..') { // if begin
$count++;
} elseif (is_dir($file)) { // end if elseif begin
$count=$count+getPictures("$dir/$file");
} // end elseif
return $count;
} // end while
// There is one end missing 
$_SERVER['DOCUMENT_ROOT'].dirname($_SERVER) // and something is missing here too
echo $count;
?>
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

Warning: opendir(/home/ozzyhead/www/./././././././././././ in /home/ozzyhead/www/goop/goop_gallery/maincounter.php on line 6

Warning: readdir(): supplied argument is not a valid Directory resource in /home/ozzyhead/www/goop/goop_gallery/maincounter.php on line 8
0

is what this code generates:

HELP!

Code: Select all

<?php
function getPictures($dir)
{ 
$handle = opendir($dir);  

while (false !== ($file = readdir($handle)))
{ 
if (is_file($file) && $file !== '.' && $file !== '..') 
{ 
$count++;
} elseif (is_dir($file)) 
{ 
$count=$count+getPictures("$dir/$file");
} 
return $count;
} 
}
$count=getPictures($_SERVER['DOCUMENT_ROOT'].dirname($_SERVER));
echo $count;
?>
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Code: Select all

<?php
function getPictures($dir)
{ 
$handle = opendir($dir);  
while (false !== ($file = readdir($handle)))
{ 
if (is_file($file) && $file !== '.' && $file !== '..') 
{ 
$count++;
} elseif (is_dir($file)) 
{ 
$count=$count+getPictures("$dir/$file");
} 
return $count;
} // are you sure you want to end while here and not earlier? 
}

$count=getPictures($_SERVER['DOCUMENT_ROOT'].dirname($_SERVER)); // something is still missing. Hint: dirname(some_dir_and_not_an_array)
echo $count;
?>
DOCUMENT_ROOT might not be the best option. Read about predefined variables.

Now I'm off to get really really drunk. It's juhannus and where I live it's customary to be senslessly drunk and not code at all during this midsummerfest.
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

I'm so much more confused now than I have ever been. Like I said before, i don't know php. I don't even know what an array is! Can anyone help? I just want the code to work...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Guys there's a problem here... is_file() returns true for driectories too...

You need is_dir() first and then is_file() ;)

Edit...

Code: Select all

//For windows use countFiles($path, '\\'), for *nix... the second param is optional.
function countFiles($path, $delim='/') {
	
	if (substr($path, -1) != $delim) {
		$path .= $delim;
	}
	
	$handle = opendir($path);
	while ($file = readdir($path)) { //Loop over the directory
	
		if ($file != '.' && $file != '..') {
			if (is_dir($path.$file)) {
				$count += countFiles($path.$file); //Recurse
			} else {
				$count++; //Count file
			}
		}
			
	}
	
	closedir($path);
	
	return $count;
	
}
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

Thanks for your reply. I used this code, but it is not generating a count...

http://www.ozzyhead.com/goop/goop_galle ... ounter.php

Code: Select all

<?php
function countFiles($path, $delim='/') {
        if (substr($path, -1) != $delim) {
        $path .= $delim;    
}        
$handle = opendir($path);
    while ($file = readdir($path)) { 
//Loop over the directory            
if ($file != '.' && $file != '..') {   
         if (is_dir($path.$file)) {       
         $count += countFiles($path.$file); 
//Recurse            
} else {                
$count++; 
//Count file          
}        
}           
}       
closedir($path);       
return $count;   
}
echo $count;
?>  Pictures
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes you need to call the function in order to get a count...

Code: Select all

function countFiles($path, $delim='/') {
        if (substr($path, -1) != $delim) {
        $path .= $delim;    
}        
$handle = opendir($path);
    while ($file = readdir($path)) { 
//Loop over the directory            
if ($file != '.' && $file != '..') {   
         if (is_dir($path.$file)) {       
         $count += countFiles($path.$file); 
//Recurse            
} else {                
$count++; 
//Count file          
}        
}           
}       
closedir($path);       
return $count;   
}
echo countFiles('/put/the/path/to/the/files/here/');
?>  Pictures
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

Should I shoot myself now?

I thought this would be fairly easy. Just trying to count the number of files in about 15 different sub-directories. Getting this error now:

Warning: readdir(): supplied argument is not a valid Directory resource in /home/ozzyhead/www/goop/goop_gallery/maincounter.php on line 9

Warning: closedir(): supplied argument is not a valid Directory resource in /home/ozzyhead/www/goop/goop_gallery/maincounter.php on line 21

Code: Select all

<?php
function countFiles($path, $delim='/') {
        if (substr($path, -1) != $delim) {
        $path .= $delim;    
}        
$handle = opendir($path);
    while ($file = readdir($path)) { 
//Loop over the directory            
if ($file != '.' && $file != '..') {   
         if (is_dir($path.$file)) {       
         $count += countFiles($path.$file); 
//Recurse            
} else {                
$count++; 
//Count file          
}        
}           
}       
closedir($path);       
return $count;   
}
echo countFiles('/home/ozzyhead/www/goop/goop_gallery/');
?>           Pictures
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:oops: Oh my god... I feel so so so so deeply ashamed.

*cough* I have used readdir($path) not $handle... corrected funtion below ;)

Code: Select all

//For windows use countFiles($path, '\\'), for *nix... the second param is optional.
function countFiles($path, $delim='/') {
    
    if (substr($path, -1) != $delim) {
        $path .= $delim;
    }
    
    $handle = opendir($path);
    while ($file = readdir($handle)) { //Loop over the directory
    
        if ($file != '.' && $file != '..') {
            if (is_dir($path.$file)) {
                $count += countFiles($path.$file); //Recurse
            } else {
                $count++; //Count file
            }
        }
            
    }
    
    closedir($handle);
    
    return $count;
    
}
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

You Rock! That works great!

...also, one other (probably very stupid) question... when displaying the results on my site, i'm calling the php script inside of an IFRAME... i know there must be a much easier way to do this...any ideas?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

In an IFRAME? Do you mean because this is a PHP files and your other files are just HTML?

Easy. Rename the HTML file to .php ---> It *would* still load as HTML even if it doesn't contain any PHP. Now that it has the .php extension simply put the code *i posted* somewhere near the top of your script inside <?php ?> tags (that just means you can now call that function whenever you want within this file) and then at the part of the page you want to display the count just add some more <?php ?> tags calling it...

Here I'll demonstrate:

Code: Select all

&lt;?php
//For windows use countFiles($path, '\\'), for *nix... the second param is optional.
function countFiles($path, $delim='/') {
    
    if (substr($path, -1) != $delim) {
        $path .= $delim;
    }
    
    $handle = opendir($path);
    while ($file = readdir($handle)) { //Loop over the directory
    
        if ($file != '.' &amp;&amp; $file != '..') {
            if (is_dir($path.$file)) {
                $count += countFiles($path.$file); //Recurse
            } else {
                $count++; //Count file
            }
        }
            
    }
    
    closedir($handle);
    
    return $count;
    
}
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Yadda yadda&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Yadda yadda yadda
&lt;p /&gt;
Guess how many files in my folder?
&lt;br&gt;
&lt;b&gt;&lt;?php

echo countFiles('/path/to/files/');

?&gt; files!!!!&lt;/b&gt;
&lt;/body&gt;
&lt;/html&gt;
In other words... php and HTML go hand-in-hand... ;)
phpnewbdude
Forum Newbie
Posts: 23
Joined: Thu Jun 23, 2005 8:07 am

Post by phpnewbdude »

Thank You, Thank You, Thank You!!!!

See it in action: http://www.ozzyhead.com
Post Reply