当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP DirectoryIterator isDir()用法及代码示例


DirectoryIterator::isDir()函数是PHP中的内置函数,用于检查当前DirectoryIterator项是否为目录。

用法:

bool DirectoryIterator::isDir( void )

参数:该函数不接受任何参数。


返回值:如果目录存在,则此函数返回TRUE,否则返回FALSE。

以下示例程序旨在说明PHP中的DirectoryIterator::isDir()函数:

示例1:

<?php 
  
// Create a directory Iterator 
$directory = new DirectoryIterator(dirname(__FILE__)); 
  
// Loop runs while directory is valid 
while ($directory->valid()) { 
  
    // Check for directory element 
    if ($directory->isDir()) { 
        $file = $directory->current(); 
  
        // Display the filename and its size 
        echo $file->getFilename() . " | Size: "
                . $directory->getSize() . "<br>"; 
    } 
  
    // Move to the next element 
    $directory->next(); 
} 
  
?>

输出:

. | Size: 4096
.. | Size: 12288
dashboard | Size: 4096
img | Size: 0
webalizer | Size: 0
xampp | Size: 0

示例2:

<?php 
  
// Create a directory Iterator 
$directory = new DirectoryIterator(dirname(__FILE__)); 
  
// Loop runs while directory is valid 
while ($directory->valid()) { 
  
    // Check for directory element 
    if ($directory->isDir()) { 
        $file = $directory->current(); 
  
        // Display file name and last modified time 
        echo $file->getFilename() . " | MTime: "
                . $directory->getMTime() . "<br>"; 
    } 
  
    // Move to the next element 
    $directory->next(); 
} 
?> 

输出:

. | MTime: 1574654324
.. | MTime: 1574540515
dashboard | MTime: 1574350724
img | MTime: 1574350724
webalizer | MTime: 1574350718
xampp | MTime: 1574350724

注意:此函数的输出取决于服务器文件夹的内容。

参考: https://www.php.net/manual/en/directoryiterator.isdir.php



相关用法


注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 PHP | DirectoryIterator isDir() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。