php小工具读取文件内容,并换算大小

李金龙
李金龙
管理员
496
文章
0
粉丝
PHP评论1,6251字数 234阅读模式

获取文件信息

  1. function get_file_info(string $filename){
  2.     //is_readabl,判断给定文件名是否存在并且可读。
  3.     //is_file判断给定文件名是否为一个正常的文件。
  4. if (!is_file($filename) || !is_readable($filename)){
  5.     return false;
  6. }
  7. return [
  8.     '上次访问时间(atime):' =>date("Y-m-d H:i:s",fileatime($filename)),
  9.     '文件上次被修改时间(mtime):' =>date("Y-m-d H:i:s",filemtime($filename)),
  10.     '文件的 inode 修改时间(ctime):'=>date("Y-m-d H:i:s",filectime($filename)),
  11.     '文件大小(size):' =>trans_byte(filesize($filename)),
  12.     '文件类型(type)' =>filetype($filename)
  13. ];
  14. }
  15. print_r(get_file_info("file_function.php"));

 

文件大小换算

  1. //封装字节转换为兆,或G
  2. //precision,默认精确度保留小数点后两位
  3. function trans_byte(int $byte,int $precision=2){
  4.     $kb = 1024;
  5.     $mb = 1024 * $kb;
  6.     $gb = 1024 * $mb;
  7.     $tb = 1024 * $gb;
  8.     if ($byte<$kb){
  9.         return $byte.'B';
  10.     }elseif ($byte<$mb){
  11.         return round($byte/$kb,$precision).'KB';
  12.     }elseif ($byte<$gb){
  13.         return round($byte/$mb,$precision).'MB';
  14.     }elseif ($byte<$tb){
  15.         return round($byte/$gb,$precision).'GB';
  16.     }else{
  17.         return round($byte/$tb,$precision).'TB';
  18.     }
  19. }

 

 
李金龙
  • 本文由 李金龙 发表于2018年1月23日 15:10:12
  • 转载请务必保留本文链接:https://www.lijinlong.cc/php/2582.html
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证