What is the recommended method in PHP to completely empty a file without deleting it?

To completely empty a file without deleting it in PHP, you can open the file in write mode, truncate its contents, and then close the file. This method will remove all the data from the file while keeping the file itself intact.

$file = 'example.txt';

if (file_exists($file)) {
    $handle = fopen($file, 'w');
    fclose($handle);
    echo 'File emptied successfully.';
} else {
    echo 'File does not exist.';
}