[SOLVED] - return # of files in multiple folders
Moderator: General Moderators
-
phpnewbdude
- Forum Newbie
- Posts: 23
- Joined: Thu Jun 23, 2005 8:07 am
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;
?>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
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!
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;
?>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;
?>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
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Guys there's a problem here... is_file() returns true for driectories too...
You need is_dir() first and then is_file()
Edit...
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
Thanks for your reply. I used this code, but it is not generating a count...
http://www.ozzyhead.com/goop/goop_galle ... ounter.php
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- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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
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
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- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
*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
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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:
In other words... php and HTML go hand-in-hand... 
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
<?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 != '.' && $file != '..') {
if (is_dir($path.$file)) {
$count += countFiles($path.$file); //Recurse
} else {
$count++; //Count file
}
}
}
closedir($handle);
return $count;
}
?>
<html>
<head>
<title>Yadda yadda</title>
</head>
<body>
Yadda yadda yadda
<p />
Guess how many files in my folder?
<br>
<b><?php
echo countFiles('/path/to/files/');
?> files!!!!</b>
</body>
</html>-
phpnewbdude
- Forum Newbie
- Posts: 23
- Joined: Thu Jun 23, 2005 8:07 am