file count in php

  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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

Post 3+ Months Ago

  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

. is the current directory and .. is the parent directory.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

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++
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

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?
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

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
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

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.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

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
  • Posts: 734

Post 3+ Months Ago

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
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

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.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

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 :)
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

No, it would still be 50 elements.

1 to 50 == 50
0 to 49 == 50
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

this is of corse not the correct way of doing it, so if you are just learning java, ignore all this... :)

49 elements starting count at 0 is 50 elements...

hence when you ant an arry with 50 elements in it you should, i claim, instantiate it by going:
Code: [ Select ]
array = new String[49]

not 50...
  • Axe
  • Genius
  • Genius
  • User avatar
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

49 elements starting count at 0 ends up at 48.

You're confusing the issue by using the word "elements" for multiple definitions :)

If I do something in PHP like...

PHP Code: [ Select ]
$somevar = array('element1', 'element2', 'element3');

then I do...

PHP Code: [ Select ]
echo $somevar[0];

$somevar[0] echo's out the 1st element. Not the 0th Element (there's no such thing as 0th).

$somevar[49] (if this array extended that far), would be the 50th element. Not the 49th.

The 49th element would be $somevar[48].
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

Very well explained. This is also explained well in Calculus using the sigma notation. You can see this page: http://home.alltel.net/okrebs/page136.html
and look at the first image. The top number, 9, is the number you would stop at. The number at the bottom, k=1, is where you would start k. But if this number was 0, you would not only go 9 times in the series, but until it reached 9, which would be 10. So if you would start at 0 and want to go to 9 numbers through the series, you would have to use 8 at the top, or use k=1, which this problem already does.
Hope I helped explain it some, and not confuse you, if Axe did not make it clear enough for you.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

i only used element for 1 definition... each individual slot in the array...

ok... if you start count at 0, and you want a total of 50 elements in your array, you would count from 0 to 49, hence 50 (0 is the first element)... agreed?

now, when you instantiate the array, you put
Code: [ Select ]
blah = new int[50]
for 50 elements... agreed?

but if you want to get the 50th element in the array, its blah[49]

my point is that the two should be the same, you should either start counting at 0 in both cases, or start at 1... not starting at 1 when you instantiate the array, and 0 when utilizing it...

argh... ok, im droping it, i am obviously not getting my point across...
  • Axe
  • Genius
  • Genius
  • User avatar
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

You are getting your point across, but your point isn't the way it works and never will be (by default) :)

Some languages, however, will allow you to start from 1 not by default, but they will allow it)...

In Pascal for example...

Code: [ Select ]
var something array[1..50] of string;

Then something[1] would be the first element in the array, something[50] would be the 50th array, and something[0] doesn't exist.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

i know its not the way it works, and that it will never be that way... that doesnt mean i have to like it though... :P

and also, 0 is a number... if you wanted an array with no elements in it it should be (according to me) something[null]
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Actually, there is a programming language that allows you to do what calendae wants to.....

microsoft visual basic :roll:

And if that isn't enough to convince you that it's a bad idea calendae then I have no idea what will :)
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

Hmmm...I'm studying VBA at the moment and my understanding of the concept replicated Axe's explanation. Particularly when it comes to Access 2000 as the base is zero.
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

good point atno. I had only just got out of bed at the time :oops: . What I meant was:

Code: [ Select ]
Dim MyArray(49) As Single


Gives you an array of 50 elements labelled 0-49, which gives the consistancy which calendae craves :lol:. I just posted in a really dumb way.
Also vb lets you do:

Code: [ Select ]
Dim MyArray(1 to 50) As Single


which I was originally taught as the proper way of doing it (i guess so they didn't confuse us with zero base indexing at the time)

Calendae - I'm not really picking on you BTW lol, I actually only really meant to get accross the point of filesystems (which is the actual topic of the thread :), but I guess it's your thread you can do what you like with it.)

As a side note atno - I did access with VBA for my school a-level projects and now I'm doing some stuff in VB6 with an external acess database. I have found the later to give a lot more freedom. I think that it allows for greater portability as the user doesn't need the same version of access as you develop in (but don't hld me to that!)
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

ya, we did sortof get away from the topic at hand didnt we... oh well... and i know youre not picking on me rtm... and either way with this radical 'the way arrays are right now is dumb' idea of mine, i probabaly deserve a little... :P

anyway, i dispise microsoft VB so much, that ill take a little inconsistancy an long as i dont have to use that... *shudders* MVB is the devil... however, i still wish that c++ and java etc were built that way... but im used to it now, so i guess it really doesnt matter... i still think its dumb though...

Post Information

  • Total Posts in this topic: 25 posts
  • Users browsing this forum: No registered users and 57 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.