What are the potential risks of using the 'w' mode in fopen() to delete file content?

Using the 'w' mode in fopen() to delete file content can be risky because it will truncate the file to zero length if it already exists, potentially causing loss of data. To avoid this risk, you can use the 'r+' mode instead, which allows you to read and write to the file without truncating it.

$file = 'example.txt';

if (file_exists($file)) {
    $handle = fopen($file, 'r+');
    ftruncate($handle, 0);
} else {
    echo "File does not exist.";
}
fclose($handle);