How can I overwrite file contents with new content in PHP?

前端 未结 3 1646
难免孤独
难免孤独 2020-12-28 11:58

I tried to use fopen, but I only managed to append content to end of file. Is it possible to overwrite all contents with new content in PHP?

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 12:26

    MY PREFERRED METHOD is using fopen,fwrite and fclose [it will cost less CPU]

    $f=fopen('myfile.txt','w');
    fwrite($f,'new content');
    fclose($f);
    

    Warning for those using file_put_contents

    It'll affect a lot in performance, for example [on the same class/situation] file_get_contents too: if you have a BIG FILE, it'll read the whole content in one shot and that operation could take a long waiting time

提交回复
热议问题