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


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


DirectoryIterator::getSize()函数是PHP中的内置函数,用于返回当前DirectoryIterator项的大小。

用法:

int DirectoryIterator::getSize( void )

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


返回值:此函数返回文件大小,以字节为单位。

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

示例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() . " | Size: "
                . $directory->getSize() . "<br>"; 
    } 
  
    // Move to next element of directory 
    $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 for each element of directory 
foreach($directory as $dir) { 
      
    $file = $directory->current(); 
      
    echo $dir->key() . " => " .  
        $file->getFilename() . " | Size: " . 
        $dir->getSize() . "<br>"; 
} 
  
?>

输出:

0 => . | Size: 4096
1 => .. | Size: 12288
2 => applications.html | Size: 3607
3 => bitnami.css | Size: 177
4 => dashboard | Size: 4096
5 => favicon.ico | Size: 30894
6 => geeks.PNG | Size: 22358
7 => gfg.php | Size: 273
8 => img | Size: 0
9 => index.php | Size: 260
10 => webalizer | Size: 0
11 => xampp | Size: 0

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

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



相关用法


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