Hi,
I'm trying to count the number of files I have in my strips folder for my comic site. I want to then put that number into a variable that will help me to automate the update process.

I tried this code to open the directory and list the files in it but didn't get much luck.

<?php
$dh = opendir("/images/strips/");
if (!$dh)
{
die("Failed to open directory");
}
$s = readdir($dh);
while ($s)
{
echo "<br>$s";
$s = readdir($dh);
}
closedir($dh);
?>

the resulting error message I get is this.

Warning: OpenDir: Invalid argument (errno 22) in d:\html\users\rwwrcom\html\test.php on line 2
Failed to open directory

So what did I do wrong? And how do I fix it?

    <?PHP
    
       $dp = opendir('images/strips') or die("Failed to open directory");
    
       while($s = readdir($dp)) {
           if (!is_dir($s)) {
         static $fileList = "";
         echo "<br>$s";
         $fileList .= " $s";
       }
    }
    closedir ($dp);
       $result = explode(" ", $fileList);
       $result2 = count($result) - 1; //Exclude current dir and current script
       echo "<BR><BR>Total :$result2";
    ?>
     

    This will display each file and then count the total amount of files in the directory. Note This script will not include sub directories, and the current directory, if you are running the script inside of images/strips, then change count($result) - 1 to - 2 instead

      OK this worked, I mean it listed all the images in the folder and gave me a correct total but it also gave me this error for each image.

      Warning: stat failed for 20020331.jpg (errno=2 - No such file or directory) in d:\html\users\rwwrcom\html\test.php on line 9

      So if line 9 is this

      if (!is_dir($s)) {

      whats wrong.

      The full script is in SpasePeepole's post above this.

        <?
        echo '<center>';
        echo '<table border=0 cellspacing=0 cellpadding=0 width=600>';
        echo '<tr><td height=10>File List Of Current Dir</td></tr>';
        $count=0;
        $handle=opendir('.');
        while (($file=readdir($handle))<>"") {
        	if(is_file($file)){
        		echo '<tr height=20><td><a href='.$file.'> '.$file.'</a></td></tr>';
        		$count++;
        	}
        }
        closedir($handle); 
        echo '</table>';
        echo '</center>';
        echo 'number of file is '.$count;
        ?>
        
          Write a Reply...