2013-08-06 14:36
浏览 29


My data.txt is generated each time when someone is visiting my site. I would like to delete this file on on specific time, let say 1:00AM daily. I found this script but I'm struggling to update the code :/ Any advice?

  $path = dirname(__FILE__).'/files';
  if ($handle = opendir($path)) {

    while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.'/'.$file)) < 86400) {  // 86400 = 60*60*24
          if (preg_match('/\.txt$/i', $file)) {
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • draj30958
    draj30958 2013-08-06 14:39

    The script you posted deletes .txt files in the given folder if they are older than a day but the problem is that this test only happens once - when you run the script.

    What you need to do is run this script periodically. If you are running this on Linux you should probably add a cron job that executes this script periodically, say once an hour or once daily.

    If you're running this on Windows, there is a task schedule that you could use to accomplish the same thing.

    点赞 评论
  • doukong9982
    doukong9982 2013-08-06 14:39

    Use task scheduler, such as cron for this purposes. You can remove your file simply via shell command

    rm /path/to/data.txt

    So there's no need to write a PHP script for that.

    点赞 评论