fwrite() 超过 2 GiB? [英] fwrite() more than 2 GiB?

查看:42
本文介绍了fwrite() 超过 2 GiB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组要连接的文件(每个文件代表多部分下载中的一部分).

I have a set of files that I want to concatenate (each represents a part from a multi-part download).

每个拆分的文件大小约为 250MiB,而且我有可变数量的文件.

Each splitted file is about 250MiB in size, and I have a variable number of them.

我的串联逻辑很简单:

if (is_resource($handle = fopen($output, 'xb')) === true)
{
    foreach ($parts as $part)
    {
        if (is_resource($part = fopen($part, 'rb')) === true)
        {
            while (feof($part) !== true)
            {
                fwrite($handle, fread($part, 4096));
            }

            fclose($part);
        }
    }

    fclose($handle);
}

我花了一段时间来追踪它,但显然,每当我有超过 8 个单独的部分(总共 2GiB)时,我的输出文件就会被截断为 2147483647 字节(由 sprintf('%u', $输出)).

It took me a while to trace it down but, apparently, whenever I have more than 8 individual parts (totaling 2GiB) my output file gets truncated to 2147483647 bytes (reported by sprintf('%u', $output)).

我想这是由于 fopen()fwrite() 使用的某种 32 位内部计数器造成的.

I suppose this is due to some kind of 32-bit internal counter used by fopen() or fwrite().

我该如何解决这个问题(最好只使用 PHP)?

How can I work around this problem (preferably using only PHP)?

推荐答案

作为一种解决方法,您可以使用 shell.如果代码必须是可移植的,那么这将只包括 Windows 和 Linux 的两个变体(也包括 MacOS).

As a workaround, you could use the shell. If the code must be portable, this would only include about two variants for Windows and Linux (covering MacOS as well).

Linux

cat file1.txt file2.txt  > file.txt

视窗

copy file1.txt+file1.txt file.txt

请注意,在创建命令行时,转义变量参数非常重要.使用 escapeshellarg() 包装文件名(参见 http://de1.php.net/escapeshellarg).

Note that when creating a command line, escaping the variable arguments is very important. Use escapeshellarg() to wrap the filenames (see http://de1.php.net/escapeshellarg).

要检测您使用的是 Windows 还是 Linux,请查看常量 PHP_OS.(最好在这里解释:http://www.php.net/manual/en/function.php-uname.php)

To detect whether you are on Windows or Linux, have a look at the constant PHP_OS. (best explained here: http://www.php.net/manual/en/function.php-uname.php)

这篇关于fwrite() 超过 2 GiB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆