0

我一直在关注有关 readdir()、is_dir() 等的教程,这些教程涉及根据我的 FTP 上的文件夹和文件设置一个小型图片库。我想知道 $directorys[] = $file; 部分具体是做什么的?

while( $file= readdir( $dir_handle ) )      
{
        if( is_dir( $file ) ){              
            $directorys[] = $file;          
        }else{                              
            $files[] = $file;               
        }
}
4

5 回答 5

3

$directory是一个数组。

编码

$directory[] = $file

添加$file$directory数组的末尾。这与

array_push($directory, $file).

更多信息在 phpdocs上的array_push

于 2012-01-09T20:21:02.630 回答
2

$file 将包含已扫描项目的名称。在这种情况下,使用 is_dir($file) 可以检查当前目录中的 $file 是否为目录。

然后,使用标准数组附加运算符 [],将 $file 名或目录名添加到 $files/$directorys 数组中...

于 2012-01-09T20:20:51.813 回答
1

它将目录添加到directory数组中:)

    if( is_dir( $file ) ){              
        $directorys[] = $file; // current item is a directory so add it to the list of directories       
    }else{                              
        $files[] = $file; // current item is a file so add it to the list of files
    }

但是,如果您使用 PHP 5,我真的建议您使用DirectoryIterator.

顺便说一句,这$file真的很糟糕,因为它并不总是一个文件。

于 2012-01-09T20:20:01.890 回答
1

它将一个项目推送到数组,而不是array_push,它只会将一个项目推送到数组。

使用array_push$array[] = $item工作原理相同,但使用起来并不理想,array_push因为它适合推送数组中的多个项目。

例子:

Array (
)

执行此操作后$array[] = 'This works!';array_push($array, 'This works!')它将显示为:

Array (
   [0] => This works!
)

您也可以将数组推送到数组中,如下所示:

$array[] = array('item', 'item2');

Array (
   [0] => Array (
             [0] => item
             [1] => item2
          )
)
于 2012-01-09T20:20:19.870 回答
0

它在数组的末尾创建一个新的数组元素并为其赋值。

于 2012-01-09T20:20:20.720 回答