PHP获取文件夹大小函数用法实例

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP获取文件夹大小函数用法。分享给大家供大家参考。具体如下:

<?php
 // 获取文件夹大小
 function getDirSize($dir)
 { 
  $handle = opendir($dir);
  while (false!==($FolderOrFile = readdir($handle)))
  { 
   if($FolderOrFile != "." && $FolderOrFile != "..") 
   { 
    if(is_dir("$dir/$FolderOrFile"))
    { 
     $sizeResult += getDirSize("$dir/$FolderOrFile"); 
    }
    else
    { 
     $sizeResult += filesize("$dir/$FolderOrFile"); 
    }
   } 
  }
  closedir($handle);
  return $sizeResult;
 }
 // 单位自动转换函数
 function getRealSize($size)
 { 
  $kb = 1024;   // Kilobyte
  $mb = 1024 * $kb; // Megabyte
  $gb = 1024 * $mb; // Gigabyte
  $tb = 1024 * $gb; // Terabyte
  if($size < $kb)
  { 
   return $size." B";
  }
  else if($size < $mb)
  { 
   return round($size/$kb,2)." KB";
  }
  else if($size < $gb)
  { 
   return round($size/$mb,2)." MB";
  }
  else if($size < $tb)
  { 
   return round($size/$gb,2)." GB";
  }
  else
  { 
   return round($size/$tb,2)." TB";
  }
 }
 echo getRealSize(getDirSize('需要获取大小的目录'));
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP实现基于回溯法求解迷宫问题的方法详解

本文实例讲述了PHP实现基于回溯法求解迷宫问题的方法。分享给大家供大家参考,具体如下: 引言 最近在leetcode上看了些算法题,有些看着很简单的很常用的东西,竟然一下子想不出来怎么求...

PHP生成UTF8文件的方法

复制代码 代码如下:<?php $f=fopen("test.txt", "wb"); $text=utf8_encode("a!"); //先用函数utf8_encode将所需写...

PHP实现微信公众号企业号自定义菜单接口示例

本文实例讲述了PHP实现微信公众号企业号自定义菜单接口。分享给大家供大家参考,具体如下: define(AppId, "wx666cae44xxxxxx2");//定义AppId,需...

php基于curl重写file_get_contents函数实例

本文实例讲述了php基于curl重写file_get_contents函数。分享给大家供大家参考,具体如下: file_get_contents在连接不上的时候会提示Connection...

php中debug_backtrace、debug_print_backtrace和匿名函数用法实例

本文实例讲述了php中debug_backtrace、debug_print_backtrace和匿名函数用法。分享给大家供大家参考。具体分析如下: debug_print_backtr...