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


how to auto empty content of text file if file size over large

>如果此文件大小超过 5kb 我将创建清除或清空内容的函数

谁能帮我

如何使用php检查文本文件的文件大小,如果文件大小超过5kb,则全部清空。

谢谢


下面是满足您要求的代码。

<?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
   }
}
?>