本文介绍了PHP fwrite()用于将大字符串写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须写一个大字符串 10MB 到文件中,我正在使用此行来实现:
I have to write a large string 10MB to file, and I am using this line to achieve that:
fwrite($file, $content);
问题是:不是整个字符串都写入文件,而是限制为特定的限制.
the problem is: not the whole string is written to the file, and limited to a specific limit.
和 fwrite 始终返回7933594
.
推荐答案
是的,fwrite
函数的长度受到限制,对于大文件,您可以将文件分割成较小的部分,如下所示:
Yes, fwrite
function is limited to length, and for a large files you may split the file to a smaller pieces like the following:
$file = fopen("file.json", "w");
$pieces = str_split($content, 1024 * 4);
foreach ($pieces as $piece) {
fwrite($file, $piece, strlen($piece));
}
fclose($file);
这篇关于PHP fwrite()用于将大字符串写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!