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


PHP DirectoryIterator __toString()用法及代碼示例


DirectoryIterator::__toString()函數是PHP中的內置函數,用於返回當前DirectoryIterator項的文件名。

用法:

string DirectoryIterator::__toString( void )

參數:該函數不接受任何參數。


返回值:此函數返回當前DirectoryIterator項的文件名。

以下示例程序旨在說明PHP中的DirectoryIterator::__toString()函數:

示例1:

<?php 
  
// Create a directory Iterator 
$directory = new DirectoryIterator(dirname(__FILE__)); 
  
// Loop runs while directory element is valid 
while ($directory->valid()) { 
      
    // Display the filename in string format 
    echo $directory->__toString() . "<br>"; 
  
    // Move to the next element 
    $directory->next(); 
} 
  
?>

輸出:

.
..
applications.html
bitnami.css
dashboard
favicon.ico
geeks.PNG
gfg.php
img
index.php
Sublime Text Build 3211 x64 Setup.exe
webalizer
xampp

示例2:

<?php 
  
// Create a directory Iterator 
$directory = new DirectoryIterator(dirname(__FILE__)); 
  
// Loop runs for each element of directory 
foreach($directory as $dir) { 
      
    // Display the key and filename 
    echo $directory->key() . " => " . 
    $directory->__toString() . "<br>"; 
} 
  
?>

輸出:

0 => .
1 => ..
2 => applications.html
3 => bitnami.css
4 => dashboard
5 => favicon.ico
6 => geeks.PNG
7 => gfg.php
8 => img
9 => index.php
10 => Sublime Text Build 3211 x64 Setup.exe
11 => webalizer
12 => xampp

注意:此函數的輸出取決於服務器文件夾的內容。

參考: https://www.php.net/manual/en/directoryiterator.tostring.php



相關用法


注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | DirectoryIterator __toString() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。