简体   繁体   English

在PHP中,为什么我的scandir调用返回错误数量的文件?

[英]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. :) 在今天的日期文件下,我还有一个其他目录,但是当要求对scandir返回的数组进行计数时,我总是得到“ 3”。(我已经用echo调用进行了检查,其中“ count(scandir($ dir))“始终返回3. $ dir中有子文件,但是有4个子文件。可能与此有关吗?如何解决此错误?感谢您提供的任何帮助;我非常感谢从有经验的程序员的建议。:)

With scandir you always get the current directory (.) and the parent directory (..). 使用scandir,您总是可以得到当前目录(。)和父目录(..)。 You can check that if you make a print_r of the result of scandir. 您可以检查是否将scandir的结果制成print_r。 So you just have to substract 2 of the count of scandir. 因此,您只需要减去scandir的计数2。

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. $ dir中有子文件,但有4个子文件。 Could that have something to do with it? 可能与这有关吗?

=> no, scandir is not recursive. =>不,scandir不是递归的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM