[SOLVED] File Counter!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

File Counter!

Post by Joe »

Is it possible to count the number of files in a directory using php?.

Regards


Joe 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

open the directory, get the file list, count them :P
[php_man]opendir[/php_man]()
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Well feyd that is exactly what I was thinking but the problem is im not to sure on how to count them. I thought of a loop but I was getting nowhere.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post the code you were trying.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

To be honest I no longer have it and im not to sure on what I done. I am positive I used a for() loop though. Im sure thats enough for you to know about what I did?.

Regards


Joe 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if using the opendir method, we most often use a while loop.
each filename returned gets the starting path added to it, as readdir only returns names. This path is then checked to see if it's a file, which will increment a counter or add it to an array.. otherwise, it ignores the path. If an array is used, after the directory is processed, you can use [php_man]sizeof[/php_man]() or [php_man]count[/php_man]() to determine how many elements are in the array (number of files in this case)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Ah ha, im sure I know where you are coming from. I will starting working on a work around immediately. Thanks feyd.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

OK Problem solved. Thanks for the help. count() done exactly what I required.
Post Reply