file count in php

  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post April 26th, 2004, 3:26 pm

is there anyway to count all the files in a folder using php?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 26th, 2004, 3:26 pm

  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post April 26th, 2004, 4:37 pm

ok, sorry for double posting... but i found my answer... i was looking around on google and found what i was looking for, and wrote this:

Code: [ Select ]
$gallery = opendir($whichSection . "/gallery/" . $who . "/");
$counter = 0;
while($file = readdir($gallery)){
    if($file != '.' && $file != '..'){
        $counter++;
    }
}
closedir($gallery);
echo($counter);
  1. $gallery = opendir($whichSection . "/gallery/" . $who . "/");
  2. $counter = 0;
  3. while($file = readdir($gallery)){
  4.     if($file != '.' && $file != '..'){
  5.         $counter++;
  6.     }
  7. }
  8. closedir($gallery);
  9. echo($counter);


it works, but now im curious... what is the file . and .. because my count is 2 greater if i remove the if statement...
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Joined: Mar 20, 2004
  • Posts: 401
  • Loc: Scorpion Hole
  • Status: Offline

Post April 26th, 2004, 4:52 pm

. is the current directory and .. is the parent directory.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post April 26th, 2004, 5:38 pm

Scorpius wrote:
. is the current directory and .. is the parent directory.


Exactly and != means "not equal to" so if $file is not equal to the current or root directory, then it executes $counter++
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post April 26th, 2004, 5:46 pm

i know what != is, and what the ++ is... its just that in my mind i was counting all the files inside the final dir, and it didnt (and still doenst) make any sense to me why the count would include them... however, i do understand what they are now, ty
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post April 26th, 2004, 6:09 pm

Let's assume that I have no idea what I'm talking about (which I don't really), but the current directory and parent directory are each going to be counted, hence your difference of two. Does that make sense?
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Joined: Mar 20, 2004
  • Posts: 401
  • Loc: Scorpion Hole
  • Status: Offline

Post April 26th, 2004, 6:16 pm

It includes them, because when you list a directory's contents it will have
.
..
file1.txt
file2.php
file3.gif
etc
and that would just mean if you went typed ..
you would go back to the parent directory in some OS's
But other times you would have to type cd ..
which would chane directory to .., parent directory.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post April 26th, 2004, 6:26 pm

Thanks Scorpius. That was what I was attempting to say, but I'm at best a novice programmer at the moment. I knew I could see what I was talking about, but just couldn't figure out how to say it. That helped me too.
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Joined: Mar 20, 2004
  • Posts: 401
  • Loc: Scorpion Hole
  • Status: Offline

Post April 26th, 2004, 6:45 pm

Heh, not a problem, although that has a little less to do with programming as it does with experimenting with multiple versions of Linux.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post April 26th, 2004, 8:22 pm

argh... what i meant by not getting it is that it was stupid, i selected the directory i wanted to count the files IN... i dont want the home directory or the current directory, i want the files IN the directory... it makes no sense... i know there is a difference between the two...

it like arrays in java... you instantiate it for the TOTAL NUMBER of elements you want... so if you want 50 elements you put in array = new String[50] blah blah blah, but to get the 50th element in that array it is array[49] its stupid... it makes no logical sense.. thats what i meant...

thanx btw... i understand it now, even if i dont like it... its just me and my teenage rebeliousness...
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Mar 24, 2004
  • Posts: 1855
  • Loc: Uk
  • Status: Offline

Post April 27th, 2004, 12:59 am

ok here is my logical justification for zero based indexing (I'm sure the theory is a little out but close enough for the principles to be right):

each element of the array is stored at the memory location.

Say we have a very small set of addresses, with 4 bits.

The highest address is, therefore 1111 (15), so therefore we can have only 15 elements in this array.

WRONG we get an extra element by using 0000.

This is logical. We are not completely ignoring a memory location, just because it doesn't suit us.


Also, my limited experience with linux has led me to believe that one of the main principles is that EVERYTHING is a file. Including your monitor or your mouse or the parent directory.

The files in a directory are not inside that directory. The directory just has a whole bunch of pointers to the places on the disc where the actual data is held. Try moving a gigabyte file to a different directory on the same logical partition. It takes no time because the data hasn't moved. By looking at it this way, you will of course need a pointer to indicate the exit from the directory (or you would never be able to get out again, or click on the "up a level" button).

Don't say that something is illogical when you haven't bothered to follow the logic through.
CSS website design tutorials
  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post April 27th, 2004, 5:15 am

my only point was that they should be consistent... this is just my opinion, feel free to disagree with me all you want...
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Mar 24, 2004
  • Posts: 1855
  • Loc: Uk
  • Status: Offline

Post April 27th, 2004, 5:27 am

I intend to :D

It is consistant... You request 50 items, and you get 50 items. The number of items is consistant. You then start counting at 0, which makes sense as I said above.

To have it completely logical, as defined by you, you would have to specify 49 elements for a 50 element array.... now that is crazy :)
CSS website design tutorials
  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post April 27th, 2004, 10:15 am

but if you start counting at 0 then 50 elements would actually be 49 elements because you started counting at 0
  • Axe
  • Genius
  • Genius
  • User avatar
  • Joined: Jan 07, 2004
  • Posts: 5744
  • Loc: Sub-level 28
  • Status: Offline

Post April 27th, 2004, 11:34 am

No, it would still be 50 elements.

1 to 50 == 50
0 to 49 == 50
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 27th, 2004, 11:34 am

Post Information

  • Total Posts in this topic: 25 posts
  • Users browsing this forum: No registered users and 169 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.