Search code examples
phpfwrite

save text file more then 8kb (80 lines) fwrite php


I am trying rewrite result.txt file after delete first row, which have total 7000 rows (700 kb) size.

Simply open the file, delete the first line then save again.

But fwrite function only save 80 rows (8kb) for me. And remaining rows auto deleted.

$outfile= "result.txt";
$o = fopen($outfile,"w");
while (!feof($handle)) {
    $buffer = fgets($handle,2048);
    fwrite($o,$buffer);
}
fclose($handle);
fclose($o);
rename($outfile, 'result.txt');

How to write big file with fwrite?


Solution

  • Problem solved by replacing w to c+

    Now code looks like this

    $outfile= "result.txt";
    $o = fopen($outfile,"c+");
    while (!feof($handle)) {
        $buffer = fgets($handle,2048);
        fwrite($o,$buffer);
    }
    fclose($handle);
    fclose($o);
    rename($outfile,$file);