php filesize 负数,php filesize() 不准确 甚至返回负数

php filesize()获取文件大小时返回的结果是错误的,甚至出现了负数。

关于filesize()返回错误结果,PHP官方有如下说明。

Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5, and minimum value using the constant PHP_INT_MIN since PHP 7.0.0.

The x64 builds of PHP 5 for Windows are experimental, and do not provide 64-bit integer or large file support.

PHP 7 provides full 64-bit support. The x64 builds of PHP 7 support native 64-bit integers, LFS, memory_limit and much more.

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

整型数的字长和平台有关,尽管通常最大值是大约二十亿(32 位有符号)。64 位平台下的最大值通常是大约 9E18。PHP 不支持无符号整数。Integer 值的字长可以用常量 PHP_INT_SIZE来表示,自 PHP 4.4.0 和 PHP 5.0.5后,最大值可以用常量 PHP_INT_MAX 来表示。

综上所述,32位PHP无法支持支持2GB以上的文件,获取2GB以上文件的大小会返回无法预知的结果,虽然PHP 5.5 PHP 5.6提供64位程序,但是只是实验性64位支持,不支持64位整型数字,也不支持2GB以上大文件。PHP 7+开始完美支持64位整型数字和大文件。建议使用64位的PHP 7.0.0以上版本,如果使用早期版本,就要使用其他方法变通解决,比如调用系统命令。除了64位支持以外,PHP 7.1开始文件系统还支持utf-8文件名和IO流,使用PHP 7.1+就再也不需要使用iconv()在gbk/utf-8等字符集之间转来转去了,文件操作更方便,代码更顺滑了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值