查看: 2610|回复: 0

[PHP实例] php实现目录大小统计的三种方法

发表于 2018-4-9 08:00:02

/
filesize 得到文件大小,返因单位为字节
unlink 删除文件
/
/echo "

";
print_r(get_declared_classes());
/

/*$dir =dir('.');
while(($file = $dir->read())!==FALSE){
echo $file."
";
}
$dir->close();
$dir->rewind();
echo "

";
while(($file = $dir->read())!==FALSE){
echo $file."
";
}*/

//echo filesize('60/a.txt')/1024;
/sleep(6);
unlink('60/1.php');
/

/sleep(5);
array_map(unlink, glob('60*.
'));*/

/function dirSize($dirname) {
$dir = opendir ( $dirname );
while ( ($file = readdir ( $dir )) !== FALSE ) {
$filename = $dirname . '\' . $file;
if ($file != '.' && $file != '..') {
if (is_dir ( $file )) {
$count += dirSize ( $file );
} else {
$count += filesize ( $filename );
}
}
}
return $count;
}
echo dirSize ( '.' ) / pow(1024,2);
/

/function dirSize_glob($dirname){
$dirInfo = glob("{$dirname}*");
foreach ($dirInfo as $v){
if(is_dir($v)){
$count +=dirSize_glob($v);
}else{
$count +=filesize($v);
}
}
return $count;
}
echo dirSize_glob('.')/pow(1024,2);
/

/function dirSize_scandir($dirname){
$dirArr = scandir($dirname);
foreach ($dirArr as $v){
$fileName = $dirname.'\'.$v;
if($v!='.' && $v!='..'){
if(is_dir($fileName)){
$count +=dirSize_scandir($fileName);
}else{
$count+=filesize($fileName);
}
}
}
return $count;
}
echo dirSize_scandir('.')/pow(1024,2);
/



回复

使用道具 举报