problem with file counting :/

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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

problem with file counting :/

Post by qads »

Code: Select all

<?php
$root = "t";
if($handle = opendir($root))
&#123;
while($file = readdir($handle))
&#123;
$count++;
&#125;
echo $count;
closedir($handle);
&#125;
?>
this counts number of files in a folder, that works fine but it is not showing the correct count. i had 2 files in the "t" folder and it showed 4, what am i doing wrong?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

A bit difference but..

Code: Select all

&lt;?php
# If you want to list the files 
# in a directory sorted by 
# date then you can use 
# the code below (probably could 
# be more efficient - but it works!)
#
$dir_name="/path/to/directory";
$dir = opendir($dir_name);
$basename = basename($dir_name);
$fileArr = array();
while ($file_name = readdir($dir))
{
 if (($file_name !=".") &amp;&amp; ($file_name != ".."))
 {
   #Get file modification date...
   #
   $fName = "$dir_name/$file_name";
   $fTime = filemtime($fName);
   $fileArr&#1111;$file_name] = $fTime;    
 }
}
# Use arsort to get most recent first
# and asort to get oldest first
arsort($fileArr);
$numberOfFiles = sizeOf($fileArr);
for($t=0;$t&lt;$numberOfFiles;$t++)
{
   $thisFile = each($fileArr);
   $thisName = $thisFile&#1111;0];
   $thisTime = $thisFile&#1111;1];
   $thisTime = date("d M y", $thisTime);
   echo"&lt;b&gt;$thisName&lt;/b&gt; $thisTime 
";
}
closedir ($dir);
?&gt;
I know there is an error in your code if you use while and "++" you have to take away "1" from the count...

Code: Select all

&lt;?php
$root = "t"; 
if($handle = opendir($root)) { 
  while($file = readdir($handle)) { 
    $count++; 
  } 
  echo $count; 
  closedir($handle); 
} 
?&gt;
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

if i take 1 away, it still leaves 3, which is still wrong! :/
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Take 2 away then :D
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

lol, what if there are 3 files? hmm? i am gona end up with 1.5 :P
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Surely this should work...

Code: Select all

&lt;?php
$count = 0;
if($handle = opendir("t")) { 
    while($file = readdir($handle)) { 
        if($file != "." || $file != "..") {
            $count++;
        }
    }   
    echo $count; 
    closedir($handle); 
} else {
    echo "Could not open folder";
}
?&gt;
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

MattF wrote:Surely this should work...

Code: Select all

&lt;?php
$count = 0;
if($handle = opendir("t")) { 
    while($file = readdir($handle)) { 
        if($file != "." || $file != "..") {
            $count++;
        }
    }   
    echo $count; 
    closedir($handle); 
} else {
    echo "Could not open folder";
}
?&gt;
Even if it does work the $count has one count extra!
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Does it? The first file it reads it adds one, that makes one, the second file it adds one, that's two... and so on...

Or am I really that bad at maths?! :(
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

No Sorry, it was me who was bad on Maths :oops:
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php 
$dir_name="t";//the folder path which you want to count the files in... 
$dir = opendir($dir_name); 
$basename = basename($dir_name); 
$fileArr = array(); 
while ($file_name = readdir($dir)) 
&#123; 
if (($file_name !=".") && ($file_name != "..")) 
&#123; 
$count++; 
&#125; 
&#125; 
closedir ($dir); 
print("$count"); 
?>

this works :D
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Just to point out that the reason behind this is that on Nix OS's, . is the current directory and .. is the previous directory, and they are both counted when you read the directory in.
Post Reply