當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。