Clear File Content in PHP: Quick Guide

To clear the contents of a file in PHP, you can use the file_put_contents function by setting its second parameter to an empty string or using the fopen function to open the file in write mode, then using the fwrite function to set the file content to an empty string.

Here are two options:

Option 1: Utilize the file_put_contents function.

$file = 'example.txt';
file_put_contents($file, ''); // 清空文件内容

Option 2: Utilize the fopen and fwrite functions.

$file = 'example.txt';
$handle = fopen($file, 'w');
fwrite($handle, ''); // 清空文件内容
fclose($handle);

This way, the content of the file can be cleared.

bannerAds