当前位置:首页 > PHP教程 > php函数 > 列表

PHP filesize() 函数

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-20 14:46:39 浏览: 评论:0 

filesize

作用:函数返回指定文件的大小

语法:

filesize(filename)

参数:

filename:必需。规定要检查的文件。

返回值:

返回文件大小的字节数,如果出错返回 FALSE 并生成一条 E_WARNING 级的错误。

filesize 示例

示例一

  1. <?php 
  2.  
  3.  
  4.  
  5. // 输出类似:somefile.txt: 1024 bytes 
  6.  
  7.  
  8.  
  9. $filename = 'somefile.txt'; 
  10.  
  11. echo $filename . ': ' . filesize($filename) . ' bytes'; 
  12.  
  13.  
  14.  
  15. ?> 

示例二

  1. <?php 
  2.  
  3. function human_filesize($bytes, $decimals = 2) { 
  4.  
  5.   $sz = 'BKMGTP'; 
  6.  
  7.   $factor = floor((strlen($bytes) - 1) / 3); 
  8.  
  9.   return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor]; 
  10.  
  11. } 
  12.  
  13. ?> 

示例三

  1. <?php 
  2.  
  3. /** 
  4.  
  5. * Converts bytes into human readable file size. 
  6.  
  7. * 
  8.  
  9. * @param string $bytes 
  10.  
  11. * @return string human readable file size (2,87 Мб) 
  12.  
  13. * @author Mogilev Arseny 
  14.  
  15. */ 
  16.  
  17. function FileSizeConvert($bytes) 
  18.  
  19. { 
  20.  
  21.     $bytes = floatval($bytes); 
  22.  
  23.         $arBytes = array( 
  24.  
  25.             0 => array( 
  26.  
  27.                 "UNIT" => "TB", 
  28.  
  29.                 "VALUE" => pow(1024, 4) 
  30.  
  31.             ), 
  32.  
  33.             1 => array( 
  34.  
  35.                 "UNIT" => "GB", 
  36.  
  37.                 "VALUE" => pow(1024, 3) 
  38.  
  39.             ), 
  40.  
  41.             2 => array( 
  42.  
  43.                 "UNIT" => "MB", 
  44.  
  45.                 "VALUE" => pow(1024, 2) 
  46.  
  47.             ), 
  48.  
  49.             3 => array( 
  50.  
  51.                 "UNIT" => "KB", 
  52.  
  53.                 "VALUE" => 1024 
  54.  
  55.             ), 
  56.  
  57.             4 => array( 
  58.  
  59.                 "UNIT" => "B", 
  60.  
  61.                 "VALUE" => 1 
  62.  
  63.             ), 
  64.  
  65.         ); 
  66.  
  67.  
  68.  
  69.     foreach($arBytes as $arItem) 
  70.  
  71.     { 
  72.  
  73.         if($bytes >= $arItem["VALUE"]) 
  74.  
  75.         { 
  76.  
  77.             $result = $bytes / $arItem["VALUE"]; 
  78.  
  79.             $result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"]; 
  80.  
  81.             break; 
  82.  
  83.         } 
  84.  
  85.     } 
  86.  
  87.     return $result; 
  88.  
  89. } 
  90.  
  91.  
  92.  
  93. ?> 

示例四

  1. <?php 
  2.  
  3. /** 
  4.  
  5. * Return file size (even for file > 2 Gb) 
  6.  
  7. * For file size over PHP_INT_MAX (2 147 483 647), PHP filesize function loops from -PHP_INT_MAX to PHP_INT_MAX. 
  8.  
  9. * 
  10.  
  11. * @param string $path Path of the file 
  12.  
  13. * @return mixed File size or false if error 
  14.  
  15. */ 
  16.  
  17. function realFileSize($path) 
  18.  
  19. { 
  20.  
  21.     if (!file_exists($path)) 
  22.  
  23.         return false; 
  24.  
  25.  
  26.  
  27.     $size = filesize($path); 
  28.  
  29.      
  30.  
  31.     if (!($file = fopen($path, 'rb'))) 
  32.  
  33.         return false; 
  34.  
  35.      
  36.  
  37.     if ($size >= 0) 
  38.  
  39.     {//Check if it really is a small file (< 2 GB) 
  40.  
  41.         if (fseek($file, 0, SEEK_END) === 0) 
  42.  
  43.         {//It really is a small file 
  44.  
  45.             fclose($file); 
  46.  
  47.             return $size; 
  48.  
  49.         } 
  50.  
  51.     } 
  52.  
  53.      
  54.  
  55.     //Quickly jump the first 2 GB with fseek. After that fseek is not working on 32 bit php (it uses int internally) 
  56.  
  57.     $size = PHP_INT_MAX - 1; 
  58.  
  59.     if (fseek($file, PHP_INT_MAX - 1) !== 0) 
  60.  
  61.     { 
  62.  
  63.         fclose($file); 
  64.  
  65.         return false; 
  66.  
  67.     } 
  68.  
  69.      
  70.  
  71.     $length = 1024 * 1024; 
  72.  
  73.     while (!feof($file)) 
  74.  
  75.     {//Read the file until end 
  76.  
  77.         $read = fread($file, $length); 
  78.  
  79.         $size = bcadd($size, $length); 
  80.  
  81.     } 
  82.  
  83.     $size = bcsub($size, $length); 
  84.  
  85.     $size = bcadd($size, strlen($read)); 
  86.  
  87.      
  88.  
  89.     fclose($file); 
  90.  
  91.     return $size; 
  92.  
  93. }

Tags: filesize

分享到:

相关文章