PHP 文件大小超过 4Gb
我正在运行 Synology NAS 服务器, 我正在尝试使用 PHP 来获取文件的文件大小。
我正在尝试找到一个能够成功计算超过 4Gb 文件的文件大小的函数。
filesize($file);
仅适用于 <2Gb
的文件 sprintf("%u", filesize($file));
仅适用于文件 <4Gb
我还尝试了在 php 手册上找到的另一个函数,但它不起作用正确地。
它随机适用于某些文件大小,但不适用于其他文件大小。
function fsize($file) {
// filesize will only return the lower 32 bits of
// the file's size! Make it unsigned.
$fmod = filesize($file);
if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);
// find the upper 32 bits
$i = 0;
$myfile = fopen($file, "r");
// feof has undefined behaviour for big files.
// after we hit the eof with fseek,
// fread may not be able to detect the eof,
// but it also can't read bytes, so use it as an
// indicator.
while (strlen(fread($myfile, 1)) === 1) {
fseek($myfile, PHP_INT_MAX, SEEK_CUR);
$i++;
}
fclose($myfile);
// $i is a multiplier for PHP_INT_MAX byte blocks.
// return to the last multiple of 4, as filesize has modulo of 4 GB (lower 32 bits)
if ($i % 2 == 1) $i--;
// add the lower 32 bit to our PHP_INT_MAX multiplier
return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
}
有什么想法吗?
I'm running a Synology NAS Server,
and I'm trying to use PHP to get the filesize of files.
I'm trying to find a function that will successfully calculate the filesize of files over 4Gb.
filesize($file);
only works for files <2Gbsprintf("%u", filesize($file));
only works for files <4Gb
I also tried another function that I found on the php manual, but it doesn't work properly.
It randomly works for certain file sizes but not for others.
function fsize($file) {
// filesize will only return the lower 32 bits of
// the file's size! Make it unsigned.
$fmod = filesize($file);
if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);
// find the upper 32 bits
$i = 0;
$myfile = fopen($file, "r");
// feof has undefined behaviour for big files.
// after we hit the eof with fseek,
// fread may not be able to detect the eof,
// but it also can't read bytes, so use it as an
// indicator.
while (strlen(fread($myfile, 1)) === 1) {
fseek($myfile, PHP_INT_MAX, SEEK_CUR);
$i++;
}
fclose($myfile);
// $i is a multiplier for PHP_INT_MAX byte blocks.
// return to the last multiple of 4, as filesize has modulo of 4 GB (lower 32 bits)
if ($i % 2 == 1) $i--;
// add the lower 32 bit to our PHP_INT_MAX multiplier
return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你溢出了 PHP 的 32 位整数。在 *nix 上,这将为您提供字符串形式的文件大小:
You are overflowing PHP's 32-bit integer. On *nix, this will give you the filesize as a string:
执行这样的 shell 命令怎么样:
其中 PATH_TO_FILE 显然是相对于 php 脚本的文件路径,
您很可能会执行一些正则表达式来获取独立的文件大小,因为它返回如下字符串:
How about executing a shell command like:
where PATH_TO_FILE is obviously the path to the file relative to the php script
you will most probably do some regex to get the filesize as a standalone as it returns a string like:
这是您可以尝试的一个完整解决方案:https://stackoverflow.com/a/48363570/2592415
Here is one complete solution what you can try: https://stackoverflow.com/a/48363570/2592415
该函数可以在32位Linux上运行
This function can run on the 32-bit Linux