PHP fwrite() for writing a large string to file

后端 未结 2 1450
野的像风
野的像风 2021-01-12 20:13

I have to write a large string 10MB to file, and I am using this line to achieve that:

fwrite($file, $content);

the proble

2条回答
  •  轮回少年
    2021-01-12 21:08

    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);
    

提交回复
热议问题