Avoid saving duplicate values when creating a file in php with special conditions [closed]

When running this code, the generated file repeats the date 4 times. Where is the problem? How can I make this command repeat only once under any condition? (I want to rewrite the previous file.)

Is there a code or command that I can run before the following code and it will be clear which function, file or code calls this part of the following code?

$curr_date = date("Y-m-d");
$filename = 'file.php';
$fp = fopen($filename, "a");
fwrite ($fp, $curr_date . "rn");
fclose ($fp);