如果文件大小过大,如何自动清空文本文件的内容

i will create function to clear or empty the content if this file size over 5kb

Can anyone help me

How to check file size of text file , and empty all if file size over 5kb ,using php.

thank you


Below is the code for your requirement.

<?php
$filename   =   'my_file.txt';
$fileSize   =   filesize($filename);
$chkSizeInBytes =   "5000"; // (5KB in bytes)
$filePtr = fopen($filename, "r+");
if ($filePtr !== false) 
{
   if($fileSize >= $chkSizeInBytes)
   {
       ftruncate($filePtr, 0); // truncates the file with size 0
       fclose($filePtr); // Close resource
   }
}
?>