PHP clear .txt file monthly

Discussion in 'PHP' started by webber09, Sep 12, 2010.

  1. #1
    hi all,
    I have a message board that is reading (and writing to) from a .html file using a PHP include. I have seen it used on another site so I know its possible, but I need a script or piece of code that clears the file monthly.

    thanks all =]
     
    webber09, Sep 12, 2010 IP
  2. Wolf Security

    Wolf Security Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Create a PHP script for it and let cron job do the rest for you.
     
    Wolf Security, Sep 12, 2010 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    Put this in your crontab, don't need php.

    0 0 1 * * echo -n > /path/to/your/file.txt
     
    Kaizoku, Sep 13, 2010 IP
  4. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4
    i dont have any access to the settings for the server this is why it must be a php script
     
    webber09, Sep 13, 2010 IP
  5. Wolf Security

    Wolf Security Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php if (date('j') == 1) { $cdel = fopen('data.html', 'w'); fclose($cdel); } ?>
    PHP:
     
    Wolf Security, Sep 13, 2010 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    Problem with that code is that, if it's executed on "landing page", the file will be wiped multiple times (probably alot of times depends on much traffic you get), you need to make sure it is only ran once on that day. And if the file was not accessed on that day, then the file won't be wiped at all.
     
    Kaizoku, Sep 15, 2010 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    Hmm.. how about deciding on how many message you want to keep, rather then deleting a whole month of messages.

    For example lets say you decided to keep the last 200 messages, when you reach that amount you could implement something like this, which will delete the oldest message each time it runs, so you would place this code after your insert / write code, in-turn a new message goes in and the oldest message gets deleted out, of course this is based on line by line and it is a txt file we are talking about right ?

    
    <?php
    
    $path = 'file.txt';
    
    $line = file($path);
    foreach ($line as $key => $value)
    {
        if ($key <= 0) {unset($line[$key]);}
        else {break;}
    }
    $line = implode("", $line);
    $handle = fopen($path, 'w');
    $size = fwrite($handle, $line);
    
    ?> 
    
    PHP:
     
    Last edited: Sep 15, 2010
    MyVodaFone, Sep 15, 2010 IP
  8. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #8
    ah great idea, thanks very much!! will be using this for sure
     
    webber09, Oct 10, 2010 IP