计算ZIP目录中的文件

时间:2013-08-09 06:46:03

标签: php

<div id="head">

<?php 
$dir = opendir('uploads/'); # This is the directory it will count from
$i = 0; # Integer starts at 0 before counting

//While false is not equal to the filedirectory
while (false !== ($file = readdir($dir))) { 
    if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
}

echo "There were $i files"; # Prints out how many were in the directory
?>
</div>

2 个答案:

答案 0 :(得分:0)

我会改用glob

$i = count(glob('uploads/*')) - count(glob('uploads/*', GLOB_ONLYDIR));

答案 1 :(得分:0)

您可以使用循环内的fnmatch()函数来匹配文件名,如下所示:

while (false !== ($file = readdir($dir))) { 
    if (fnmatch('*.zip', $file)) {
        ++$i;
    }
}

glob()也能理解相同的模式。