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


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


DirectoryIterator::getType()函数是PHP中的内置函数,用于检查当前DirectoryIterator项的类型。

用法:

string DirectoryIterator::getType( void )

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


返回值:此函数返回一个表示文件类型的字符串。类型可以是文件,链接或目录之一。

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

示例1:

<?php 
  
// Create a directory Iterator 
$directory = new DirectoryIterator(dirname(__FILE__)); 
  
// Loop runs while directory is valid 
while ($directory->valid()) { 
  
    // Check it is directory or not 
    if ($directory->isDir()) { 
        $file = $directory->current(); 
        echo $file->getFilename() . " | Type: "
                . $directory->getType() . "<br>"; 
    } 
  
    // Move to the next element of DirectoryIterator 
    $directory->next(); 
} 
  
?>

输出:

. | Type: dir
.. | Type: dir
dashboard | Type: dir
img | Type: dir
webalizer | Type: dir
xampp | Type: dir

示例2:

<?php 
  
// Create a directory Iterator 
$directory = new DirectoryIterator(dirname(__FILE__)); 
  
// Loop runs for each element of directory 
foreach($directory as $dir) { 
      
    $file = $directory->current(); 
      
    echo $dir->key() . " => " .  
        $file->getFilename() . " | Type: " . 
        $dir->getType() . "<br>"; 
} 
  
?>

输出:

0 => . | Type: dir
1 => .. | Type: dir
2 => applications.html | Type: file
3 => bitnami.css | Type: file
4 => dashboard | Type: dir
5 => favicon.ico | Type: file
6 => geeks.PNG | Type: file
7 => gfg.php | Type: file
8 => img | Type: dir
9 => index.php | Type: file
10 => webalizer | Type: dir
11 => xampp | Type: dir

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

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



相关用法


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