php定时删除文件或清空文件内容

admin 2023-12-06 45 阅读 0评论
<?php
/**
* 超过规则时间清空文件内容
*/
//当前时间
$now = time();
//过期时间 秒***(60*60*24*6) 根据需求设置
$stime = 10;
$see = time() + $stime;
//生成的文件名
$fname = 'decodes.php';
$redf = '';
//判断文件是否存在
if (file_exists($fname)) {
         //读取文件
   $redf = file_get_contents($fname);
         //字符串转为整数
   $redf = intval($redf);
} else {
         //生成时间戳文件
        file_put_contents($fname, $see);
}

//判断文件是否为空
if ($redf) {
      //当前时间大于等于过期时间
  if ($now >= $redf) {
       //清空文件内容(或者删除文件)
       file_put_contents(php_self(), '');
                   //删除时间戳文件
       unlink($fname);
      }
} else {
       //生成时间戳文件
      file_put_contents($fname, $see);
}

//获取当前文件名
function php_self()
{
   $php_self = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1);
   return $php_self;
}


echo '<br>';
echo '当前时间:' . date('Y-m-d H:i:s', $now) . '-->' . $now . '<hr>';
echo '过期时间:' . @date('Y-m-d H:i:s', $redf) . '-->' . $redf . '<br>';
echo '当前文件名:' . php_self() . '<br>';

发表评论

快捷回复: 表情:
Addoil Applause Badlaugh Bomb Coffee Fabulous Facepalm Feces Frown Heyha Insidious KeepFighting NoProb PigHead Shocked Sinistersmile Slap Social Sweat Tolaugh Watermelon Witty Wow Yeah Yellowdog
提交
评论列表 (有 0 条评论, 45人围观)