PHP implementation of statistical directory file size function _php skills

Source: Internet
Author: User
Tags pow

Morning just to the company, the head told me, to write a small function, used to statistics specified in the directory file size, I have a go, do it, fortunately a little foundation, a will be completed, haha. The code is below.

? 
/** 
 Statistics Directory file size function 
 @author xfcode 
* 
 /function dirsize ($dir) 
 { 
@ $dh = Opendir ($dir); 
 $size = 0; 
 while ($file = @readdir ($DH)) 
{ 
 if ($file!= "." and $file!= "...") 
{ 
 $path = $dir. "     /". $file; 
 if (Is_dir ($path)) 
{ 
$size + = Dirsize ($path); 
 } 
ElseIf (Is_file ($path)) 
{ 
 $size + + = filesize ($path); 
 @closedir ($DH)}}}; return 
 $size; 
 } 
Function End 
 
//eg: 
 $dir _path = "./my_files"; 
 $dir _size = dirsize ($dir _path); 
 $dir _size = $dir _size/1024/1024; 
 Echo $dir _size. " MB "; 
 ? > 

This function recursively iterates through all the files in the directory and calculates the total size of the files in megabytes.

Let's take a look at the code here.

<?php #循环遍历目录中所有的文件, and statistics the size of directories and files $dirName = "phpMyAdmin"; $dir =opendir ($dirName); #返回一个资源类型 while ($fileName =readdir ($dir)) {$file = $dirName. "
 /". $fileName; if ($fileName!= "." && $fileName!= "...") {if (Is_dir ($file)) {echo "<font color= ' Red ' >". $fileName. " = = = ". Date (" Y-m-d h:i:s ", Filectime ($file))." = = ". FileType ($file)." = = ". Tosize (Dirsize ($file))."
  </font><br/> "; else{echo "<font color= ' green ' >". $fileName. " ===== ". Date (" Y-m-d h:i:s ", Filectime ($file))." = = = ". FileType ($file)." = = = ". Tosize (FileSize ($file))."
  </font><br/> ";
 
 }} closedir ($dir);
  #把文件或目录的大小转化为容易读的方式 function Tosize ($size) {$dw; #指定文件或目录统计的单位方式 if ($size >pow (2,30)) {$DW = "GB";
 $size =round ($size/pow (2,30), 2);
  else if ($size >pow (2,20)) {$DW = "MB";
 $size =round ($size/pow (2,20), 2);
  else if ($size >pow (2,10)) {$DW = "KB";
 $size =round ($size/pow (2,10), 2);
 else {$dw = "bytes";
 return $size. $dw; #利用递归的方式统计目录的大小 function Dirsize ($dirName) {$dirsize = 0;
 $dir =opendir ($dirName); while ($fileName =readdir ($dir)) {$file = $dirName. "
  /". $fileName; if ($fileName!= "." && $fileName!= "...")
  {//must be judged, otherwise there will be an error if (Is_dir ($file)) {$dirsize +=dirsize ($file);
  else{$dirsize +=filesize ($file);
 }} closedir ($dir);
 return $dirsize; }?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.