簡體   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

在今天的日期文件下,我還有一個其他目錄,但是當要求對scandir返回的數組進行計數時,我總是得到“ 3”。(我已經用echo調用進行了檢查,其中“ count(scandir($ dir))“始終返回3. $ dir中有子文件,但是有4個子文件。可能與此有關嗎?如何解決此錯誤?感謝您提供的任何幫助;我非常感謝從有經驗的程序員的建議。:)

使用scandir,您總是可以得到當前目錄(。)和父目錄(..)。 您可以檢查是否將scandir的結果制成print_r。 因此,您只需要減去scandir的計數2。

根據文檔:

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

print_r($files1);

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

$ dir中有子文件,但有4個子文件。 可能與這有關嗎?

=>不,scandir不是遞歸的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM