Damphair
Damphair

Reputation: 1

In PHP, why does my scandir call return the wrong amount of files?

    for($instancesFiles=1;$instancesFiles<count(scandir("Articles/".date('y-m-d').""));$instancesFiles++){
//For each file inside the directory for today's articles, each of which corresponds to an instance of the "article" class...
        $dir="Articles/".date("y-m-d")."/".$instancesFiles."";
        $artProps=scandir($dir);
    //Retrieve an array of its interior files, each of which corresponds to a property of the aforementioned instance

Under the file of today's date, I have a single other directory, but when asking for the count the array returned by scandir, I always get "3".(I've checked this with echo calls, where "count(scandir($dir))" always returns 3. There are children files within $dir, but there are 4 children. Could that have something to do with it? How can I fix this error? Thanks for any help you can offer; I really appreciate getting advice from more experienced programmers. :)

Upvotes: 0

Views: 452

Answers (1)

With scandir you always get the current directory (.) and the parent directory (..). You can check that if you make a print_r of the result of scandir. So you just have to substract 2 of the count of scandir.

As per the doc :

$dir    = '/tmp';
$files1 = scandir($dir);

print_r($files1);

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)

There are children files within $dir, but there are 4 children. Could that have something to do with it?

=> no, scandir is not recursive.

Upvotes: 2

Related Questions