PHP fwrite() for writing a large string to file

后端 未结 2 1452
野的像风
野的像风 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条回答
  •  梦毁少年i
    2021-01-12 21:11

    Alternative way of @Ayman Alkom solution.

    function fwrite_stream($fp, $string) {
        for ($written = 0; $written < strlen($string); $written += $fwrite) {
            $fwrite = fwrite($fp, substr($string, $written));
            if ($fwrite === false) {
                return $written;
            }
        }
        return $written;
    }
    

    This should make a bit better performance.

    But if you use this code for copy a big file,

    Linux Command

    "cat file1.txt file2.txt > file.txt" 
    

    Window Command

    "copy file1.txt+file1.txt file.txt"
    

    Is the sollution.

提交回复
热议问题