Jump to content


Photo

Counting Files in a directory


  • Please log in to reply
26 replies to this topic

#1 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 04:53 PM

Hi,

I'm using this script to count the number of files in a directory (e.g. I want to display: '10 Files hosted')

The problem is that this code is returning '0' as the count, which is wrong.

The index page in the root will have the counter on (www.domain.com)

the directory with the files in is www.domain.com/1

//get path of directory
$dir = ("/1/"); 
//open a handle to the directory
$handle = opendir($dir);
//intitialize our counter
$count = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
  if (is_file($file) && $file !== '.' && $file !== '..') {
    $count++;
  }
}
echo $count;

Any help would be much appreciated!

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 05:18 PM

Is that a valid directory path. Put checks. Example:

//get path of directory
$dir = '/1/';   // or maybe you meant './1/' or '/home/usr/www/1/'

if (!is_dir($dir)) {
    echo $dir, ' is not a directory or is not found.';
    exit;
}

//open a handle to the directory
$handle = opendir($dir);

if (!$handle) {
    echo $dir, ' could not be opened.';
    exit;
}

//intitialize our counter
$count = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
  if ((is_file($file)) && ($file !== '.') && ($file !== '..')) {
    $count++;
  }
}
echo $count;

EDIT:

Try using:

$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/';


#3 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 05:24 PM

If I put that in I still get '0'



#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 05:26 PM

[!--quoteo(post=375808:date=May 21 2006, 10:24 AM:name=QuadVods)--][div class=\'quotetop\']QUOTE(QuadVods @ May 21 2006, 10:24 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If I put that in I still get '0'

Take a look: www.digitaluploader.com

The number should be along the bottom
[/quote]
Put what exactly?

I edited my previous post. Use $_SERVER.

While testing/debugging this, make sure you have error_reporting(E_ALL); at the top of your script so you can see all of PHP's errors/warnings/notices. Also, I assume you have display_errors on in the php.ini file. Otherwise, set it using ini_set('display_errors', '1'); at the top of your script too.

#5 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 05:32 PM

Nope - I tried both the edits you mention, I just ge a '0'. I've got shared hosting, so I cant change any php settings.

It is worth mentioning that the original script works and counts the files in the directory it is placed in...


//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 = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
  if (is_file($file) && $file !== '.' && $file !== '..') {
    $count++;
  }
}
echo $count;


#6 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 05:38 PM

1) You can change some php.ini settings at run time.
2) Don't use $PHP_SELF, unless you know register_globals is on. Use $_SERVER['PHP_SELF'] instead.
3) It's just a matter of finding what your path is. Do a phpinfo and find out.

FYI:

I just managed to upload a Windows .exe file disguised as .jpg.

You can delete the one I just uploaded:
[a href=\"http://www.DigitalUploader.com/viewer.php?id=7449application.jpg\" target=\"_blank\"]http://www.DigitalUploader.com/viewer.php?...application.jpg[/a]

See this post for how to prevent this sort of thing:
[a href=\"http://www.phpfreaks.com/forums/index.php?s=&showtopic=93371&view=findpost&p=373601\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...ndpost&p=373601[/a]

#7 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 05:43 PM

edit

Does that help?

Thanks for the other info - i'll check that out

#8 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 05:49 PM

Well, this should work (as I posted before):

// Becomes: /kunden/homepages/23/d163968312/htdocs/upload/1/
$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/';

Don't use 'php_self' because you're script is probably not running in the '/1/' directory.

#9 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 05:52 PM

[!--quoteo(post=375819:date=May 21 2006, 06:49 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 21 2006, 06:49 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, this should work (as I posted before):

$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/'; // /kunden/homepages/23/d163968312/htdocs/upload/1/

Don't use 'php_self' because you're script is probably not running in the '/1/' directory.
[/quote]


It still just says 0...

There are 8 files in the directory at this moment.

And yes - the script isnt in the 1 directory.

#10 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 05:59 PM

[!--quoteo(post=375821:date=May 21 2006, 10:52 AM:name=QuadVods)--][div class=\'quotetop\']QUOTE(QuadVods @ May 21 2006, 10:52 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
It still just says 0...

There are 8 files in the directory at this moment.

And yes - the script isnt in the 1 directory.
[/quote]
The last code you posted didn't include other stuff I've recommended you do. So, display the $dir value, include the is_dir(), check the $handle variable, and put echo's in the while loop to help yourself debug this problem.

Good luck.


#11 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 06:05 PM

[!--quoteo(post=375824:date=May 21 2006, 06:59 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 21 2006, 06:59 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The last code you posted didn't include other stuff I've recommended you do. So, display the $dir value, include the is_dir(), check the $handle variable, and put echo's in the while loop to help yourself debug this problem.

Good luck.
[/quote]

The last code was the original script before I edited it.

So this is what I have to far:


//get path of directory
$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/'; // /kunden/homepages/23/d163968312/htdocs/upload/1/

if (!is_dir($dir)) {
    echo $dir, ' is not a directory or is not found.';
    exit;
}

//open a handle to the directory
$handle = opendir($dir);

if (!$handle) {
    echo $dir, ' could not be opened.';
    exit;
}

//intitialize our counter
$count = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
  if ((is_file($file)) && ($file !== '.') && ($file !== '..')) {
    $count++;
  }
}
echo $count;


It still just retuns the '0'.


#12 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 06:25 PM

humor me. move $count++ outside the if statement like so:

//intitialize our counter
$count = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
  if ((is_file($file)) && ($file !== '.') && ($file !== '..')) {
    // $count++;
  }
 $count++;
}
echo $count;

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#13 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 06:25 PM

Well, I don't know what's going on. Put displays in the while loop to see if it's getting in there. There's also a command to clear out the directory buffer because PHP cache's that, but I can't remember it's name. If I do, I'll post it.

You could try a different approach:

// Becomes: /kunden/homepages/23/d163968312/htdocs/upload/1/
$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/';

$ary = glob($dir . '*.*');   // Needs 4.3.0+ which you have

echo 'File count: ', count($ary), '<br/>';


EDIT:

FYI: clearstatchache():
[a href=\"http://us2.php.net/manual/en/function.clearstatcache.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.clearstatcache.php[/a]



#14 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 06:32 PM

[!--quoteo(post=375834:date=May 21 2006, 07:25 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 07:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
humor me. move $count++ outside the if statement like so:

//intitialize our counter
$count = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
  if ((is_file($file)) && ($file !== '.') && ($file !== '..')) {
    // $count++;
  }
 $count++;
}
echo $count;
[/quote]



[img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] It works, It works! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Thanks very much both of you!

#15 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 06:35 PM

umm, well then shouldn't it now be returning the count, including . and .. ?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#16 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 06:37 PM

[!--quoteo(post=375841:date=May 21 2006, 07:35 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 07:35 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
umm, well then shouldn't it now be returning the count, including . and .. ?
[/quote]


Well the counter is 2 out of the actual figure.. but thats close enough for me! If you know why that is.. that would be super.

Code I have now:


//get path of directory
$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/';

//open a handle to the directory
$handle = opendir($dir);

if (!$handle) {
    echo $dir, ' could not be opened.';
    exit;
}

//intitialize our counter
$count = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
  if ((is_file($file)) && ($file !== '.') && ($file !== '..')) {
    // $count++;
  }
$count++;
}
echo $count;


#17 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 06:38 PM

[!--quoteo(post=375840:date=May 21 2006, 11:32 AM:name=QuadVods)--][div class=\'quotetop\']QUOTE(QuadVods @ May 21 2006, 11:32 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
[img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] It works, It works! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Thanks very much both of you!
[/quote]
I wouldn't get excited just yet. Is the count right?

I would change this:

if ((is_file($file)) && ($file !== '.') && ($file !== '..')) {
$count++;
}

to this:

if ((is_file($file)) && ($file != '.') && ($file != '..')) {
$count++;
}

Of course take the count outside the 'if' statment out. Then see what the count is.

Otherwise, just use the glob() function as demonstrated in one of my earlier posts.

#18 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 06:40 PM

i think you said earlier you had 8 files, right? so it's now returning 10, right? it's because it's counting the '.' and '..' as files. the problem is with that if statement. the easy ghetto solution to this is to simply subtract 2 from count.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#19 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 21 May 2006 - 06:43 PM

[!--quoteo(post=375844:date=May 21 2006, 11:40 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 11:40 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i think you said earlier you had 8 files, right? so it's now returning 10, right? it's because it's counting the '.' and '..' as files. the problem is with that if statement. the easy ghetto solution to this is to simply subtract 2 from count.
[/quote]
Ah, cheating instead of understanding why it's doing what it's doing. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Hey, do what you want. But the !== is more restrictive than just != since it compares the type too.



#20 QuadVods

QuadVods
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 May 2006 - 06:46 PM

@toplay:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]if ((is_file($file)) && ($file != '.') && ($file != '..')) {
$count++;
}[/quote]

Still gives 2 more files than I have.


I'm up for the ghetto method if you don't want to worry about it.. help with that or a true fix would be great.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users