Page 2 of 2
Posted: Fri Jun 24, 2005 7:39 am
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.
Posted: Fri Jun 24, 2005 7:46 am
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;
?>
Posted: Fri Jun 24, 2005 7:51 am
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;
?>
Posted: Fri Jun 24, 2005 8:12 am
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;
?>
Posted: Fri Jun 24, 2005 8:22 am
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.
Posted: Fri Jun 24, 2005 8:30 am
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...
Posted: Fri Jun 24, 2005 8:55 am
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;
}
Posted: Fri Jun 24, 2005 10:10 am
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
Posted: Fri Jun 24, 2005 10:50 am
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
Posted: Fri Jun 24, 2005 11:04 am
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
Posted: Fri Jun 24, 2005 11:10 am
by Chris Corbyn

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;
}
Posted: Fri Jun 24, 2005 11:30 am
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?
Posted: Fri Jun 24, 2005 11:48 am
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
<?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>
In other words... php and HTML go hand-in-hand...

Posted: Fri Jun 24, 2005 2:15 pm
by phpnewbdude
Thank You, Thank You, Thank You!!!!
See it in action:
http://www.ozzyhead.com