fwrite()超过2个GiB? [英] fwrite() more than 2 GiB?
问题描述
每个拆分的文件的大小大约为250MiB ,我有一个可变的数字。
我的连接逻辑是直截了当的: 我花了一段时间才找到它,但显然,只要我有我的输出文件被截断为2147483647个字节(由 我想这是由于某些由 如何解决这个问题(最好只使用PHP)? =h2_lin>解决方案
pre > 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);
$ b $ p
$ b sprintf('%u',$ output)
)报告。 b
$ b 使用的32位内部计数器fopen()
或 fwrite ()
。
作为一种解决方法,您可以使用shell。如果代码必须是可移植的,那么这将只包括Windows和Linux的两个变种(包括MacOS)。
Linux
cat file1.txt file2.txt> file.txt
Windows
copy file1.txt + file1.txt file.txt
请注意,创建命令行,转义可变参数非常重要。使用 escapeshellarg()
来包装文件名(参见 http://为了检测你是在Windows还是在Linux上,可以看一看常量 PHP_OS
。 (最好在这里解释: http://www.php.net/manual/ en / function.php-uname.php )
I have a set of files that I want to concatenate (each represents a part from a multi-part download).
Each splitted file is about 250MiB in size, and I have a variable number of them.
My concatenation logic is straight-forward:
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);
}
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)
).
I suppose this is due to some kind of 32-bit internal counter used by fopen()
or fwrite()
.
How can I work around this problem (preferably using only PHP)?
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
Windows
copy file1.txt+file1.txt file.txt
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).
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屋!