代码大全-php
获取磁盘空间
1 <?php 2 function calc_size($dir){//Calculates the disk size used by the directory, including all sub-folders and files in it. 3 if(is_file($dir)){ 4 return filesize($dir); 5 }elseif(!is_dir($dir)){ 6 return 0; 7 } 8 $size=0; 9 @$dh=opendir($dir); //get dir handler 10 while($file=@readdir($dh)){ //loop files and sub-folders under this directory 11 if($file!="." and $file != ".."){//not the current or upper directory 12 $path=$dir."/".$file; 13 if(is_dir($path)){ 14 $size+=calc_size($path); //recursive invocation 15 }elseif(is_file($path)){ 16 $size+=filesize($path); 17 } 18 } 19 } 20 @closedir($dh); 21 return $size; 22 } 23 24 #use example: 25 $dir="."; 26 $dir_size=calc_size("$dir"); 27 echo "Total size for \"".realpath($dir)."\": ".$dir_size." BYTE."; 28 ?>
删除指定目录
1 <?php 2 function full_rmdir($dir){//Fully removes the directory, including all sub-folders and files in it. 3 if(is_file($dir)){ 4 return unlink($dir); 5 }elseif(!is_dir($dir)){ 6 return false; 7 } 8 9 if($dh=opendir($dir)){ 10 $old_cwd=getcwd(); 11 chdir($dir); 12 13 while ($file=readdir($dh)) { 14 if($file=='.' || $file=='..') continue; 15 if (is_dir($file)) { 16 if(!full_rmdir($file)) return false; 17 }else{ 18 if(!unlink($file)) return false; 19 } 20 } 21 22 closedir($dh); 23 chdir($old_cwd); 24 if(!rmdir($dir)) return false; 25 26 return true; 27 }else{ 28 return false; 29 } 30 31 } 32 33 #use example: 34 $dir="test"; 35 if(full_rmdir("$dir")){ 36 echo "Delete succeed!"; 37 }else{ 38 echo "Delete failed!"; 39 } 40 41 ?>
持续更新中
持续更新中
持续更新中
浙公网安备 33010602011771号