百科狗-知识改变命运!
--

filesize() - 取得文件大小 - php 文件目录函数

梵高1年前 (2023-11-21)阅读数 10#技术干货
文章标签函数

filesize()

(PHP 4, PHP 5, PHP 7)

取得文件大小

说明

filesize(string $filename): int

取得指定文件的大小。

参数

$filename

文件的路径。

返回值

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

filesize() - 取得文件大小 - php 文件目录函数

Note:因为 PHP 的整数类型是有符号整型而且很多平台使用 32 位整型,对 2GB以上的文件,一些文件系统函数可能返回无法预期的结果。

范例

Example #1filesize()例子

错误/异常

失败时抛出E_WARNING警告。

注释

Note:此函数的结果会被缓存。参见clearstatcache()以获得更多细节。

Tip

自 PHP 5.0.0 起,此函数也用于某些URL 包装器。请参见支持的协议和封装协议以获得支持stat()系列函数功能的包装器列表。

参见

  • file_exists() 检查文件或目录是否存在
Extremely simple function to get human filesize. 
function dir_size($file) {
  //tested on win 7x64 php 5.4 
  exec('dir /s /a "' . $file.'"', $inf);
  $r=explode(' ',$inf[count($inf)-2]);
  $rr = preg_replace('~[^\d]+~','',$r[count($r)-2]);
  return $rr;
}
if you recently appended something to file, and closed it then this method will not show appended data:

You should insert a call to clearstatcache() before calling filesize()
I've spent two hours to find that =/
// best converting the negative number with File Size . 
// does not work with files greater than 4GB
//
// specifically for 32 bit systems. limit conversions filsize is 4GB or 
// 4294967296. why we get negative numbers? by what the file 
// pointer of the meter must work with the PHP MAX value is 2147483647.
// Offset file : 0 , 1 , 2 , 3 , ... 2147483647 = 2GB 
// to go higher up the 4GB negative numbers are used
// and therefore after 2147483647, we will -2147483647
// -2147483647, -2147483646, -2147483645, -2147483644 ... 0 = 4GB
// therefore 0, 2147483647 and -2147483647 to 0. all done 4GB = 4294967296
// the first offset to 0 and the last offset to 0 of 4GB should be added in 
// your compute, so "+ 2" for the number of bytes exate . 
Slightly edited version of the function from rommel at rommelsantor dot com. Now it returns a two characters file size which is a bit more convenient to read.

I removed the P units because strlen doesn't seem to work as expected with integers longer than 14 digits. Though it might be only my system's limitation.
For files bigger then 2 GB use my library called Big File Tools. https://github.com/jkuchar/BigFileTools. More details on stackoverflow: http://stackoverflow.com/a/35233556/631369
The simplest and most efficient implemention for getting remote filesize: 
In case of you have a redirection in the server (like Redirect Permanent in the .htaccess)
In this case we have for exemple:
  [content-length] => Array
    (
      [0] => 294     // Size requested file
      [1] => 357556   // Real Size redirected file
    )
This functions returns the exact file size for file larger than 2 GB on 32 bit OS:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)