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

前端 未结 3 1640
难免孤独
难免孤独 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:15

    $fname = "database.php";
    $fhandle = fopen($fname,"r");
    $content = fread($fhandle,filesize($fname));
    $content = str_replace("192.168.1.198", "localhost", $content);
    
    $fhandle = fopen($fname,"w");
    fwrite($fhandle,$content);
    fclose($fhandle);
    

提交回复
热议问题