Page 1 of 1

problem with file counting :/

Posted: Sun Sep 08, 2002 1:33 pm
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?

Posted: Sun Sep 08, 2002 1:50 pm
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;

Posted: Sun Sep 08, 2002 2:04 pm
by qads
if i take 1 away, it still leaves 3, which is still wrong! :/

Posted: Sun Sep 08, 2002 2:19 pm
by Takuma
Take 2 away then :D

Posted: Sun Sep 08, 2002 2:23 pm
by qads
lol, what if there are 3 files? hmm? i am gona end up with 1.5 :P

Posted: Sun Sep 08, 2002 2:35 pm
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;

Posted: Sun Sep 08, 2002 2:41 pm
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!

Posted: Sun Sep 08, 2002 3:07 pm
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?! :(

Posted: Sun Sep 08, 2002 3:26 pm
by Takuma
No Sorry, it was me who was bad on Maths :oops:

Posted: Sun Sep 08, 2002 6:14 pm
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

Posted: Sun Sep 08, 2002 7:58 pm
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.