PHP文件操作

  1.递归获取目录下文件的个数

function getFileCount($dir)
{

if(!is_dir($dir))
return false;
//打开目录
$handle = opendir($dir);
static $i = 0 ;
while(false !== $file = readdir($handle))
{
if($file != '.' && $file!== '..')
{
$file = $dir.DIRECTORY_SEPARATOR .$file;
//判断当前文件是否是一个目录
if(is_dir($file))
{
getFileCount2($file);

}else
$i++;
}
}
closedir($handle);
return $i;
}

2.递归获取目录的大小
function getFileSize($dir)
{
if(!is_dir($dir))
return false;
//打开目录
$handle = opendir($dir);
static $num = 0 ;
while(false !== $file = readdir($handle))
{
if($file != '.' && $file!== '..')
{
$file = $dir.DIRECTORY_SEPARATOR .$file;
//判断当前文件是否是一个目录
if(is_dir($file))
{
getFileSize($file);

}else
$num += filesize($file);
}
}
closedir($handle);
return $num;
}


原文地址:https://www.cnblogs.com/da-guang/p/4883218.html