好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

4、PHP获取文件大小并格式化

以下使用的函数可以获取文件的大小,并且转换成便于阅读的KB,MB等格式。

  1   function  formatSize( $size  ) { 
   2  
  3       $sizes  =  array (" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB" ); 
   4  
  5       if  ( $size  == 0 ) {  
   6  
  7           return ('n/a' );  
   8  
  9      }  else   { 
  10  
 11         return  ( round ( $size / pow (1024, ( $i  =  floor ( log ( $size , 1024)))), 2) .  $sizes [ $i  ]);  
  12  
 13       } 
  14  
 15  }

使用方法如下:

 1   $thefile  =  filesize ('test_file.mp3' ); 
  2  
 3   echo  formatSize( $thefile );

 


查看更多关于4、PHP获取文件大小并格式化的详细内容...

  阅读:44次