Hi,

I am using a free web host and it's PHP version is 4.3.1, and the scandir() function was introduced in 5. Is there an alternative?
My goal is to count the number of files in a folder and display them, e.g.

<?php
// Code
echo "<b>Total Files in <i>directory</i>:</b> ".count(scandir('directory'))-2;
// Code
?>

only obviously without the scandir() part. Is there an alternative?

Thanks,
Lupus

    Uh... try reading the manual for [man]scandir/man. Perhaps this sounds like it might possibly be useful: "Example 2. PHP 4 alternatives to scandir()" ?

      I'm really sorry, I did read the manual on the usage of scandir(), but I didn't even know there was an Alternative thing.

      Thanks.

        OK, but how could you then count the files with this function?

          lupus2k5 wrote:

          OK, but how could you then count the files with this function?

          Using the second example given in the PHP manual for scandir(), using the function count() will return the number of elements in an array.

            No, that would be count(scandir()). I need to use the 4.x alternative....
            Or, as NogDog mentioned, glob(). But can I count with glob()?

              lupus2k5 wrote:

              Or, as NogDog mentioned, glob(). But can I count with glob()?

              Yes, as long as you remember that glob() returns "." (current directory) and ".." (parent directory) entries, so if you did a count(glob('.')) and got 5, then you know that there are 3 files.

                So to return the number of files, I could use

                <?php
                $directory = "PATH/TO/DIRECTORY";
                $numFiles = count(glob($directory)) -2;
                echo "The number of files in ".$directory." is ".$numFiles.".";
                ?>
                

                ?

                  Write a Reply...