0

I have a text file, that stores chat log and I want the content to be cleared automaticly by the server when the clock is 00:00:00.000, or let’s say at a certain time, so that it’s cleared every 24 hours. I found something similar here but I want the server to automaticly clear not delete the txt file.

Community
  • 1
  • 1
  • Just replace the code in the link you've given us, with a replacing function. Or, in SQL you can use an UPDATE and replace with the blank content or content of your choice. – Funk Forty Niner Jun 02 '14 at 14:27
  • Well, I found another solution, deleting the file if 24H old, then replace with a new, empty –  Jun 02 '14 at 14:29
  • But I don't know how though, and I don't want to delete every file. –  Jun 02 '14 at 14:29
  • You can just write "nothing" to file using `fwrite` with the `w` switch or `file_put_contents`. Easy as pie. But CRON seems to be the answer to do this every X-amount of time. – Funk Forty Niner Jun 02 '14 at 14:32
  • What is your operating system? Without knowing that nobody can give you specific help. – Giacomo1968 Jun 02 '14 at 14:35
  • one.com servers, I have Windows 8.1 though –  Jun 02 '14 at 14:40

2 Answers2

2

If you want it to run automatically you can set up a cronjob.

Or if you are on Windows use the Task Scheduler.

Using this you can run for example a php file on a specific time using:

php -f /path/to-your/file

An example of a line in the crontab will look like:

0  0  *  *  *  php -f /path/to-your/file

The above will run the script every day at 00:00

Hirudinea
  • 61
  • 3
  • German? I wonder, isn't there anything simpler? –  Jun 02 '14 at 14:31
  • Ooh nice command there, but where do I type it (nOOb) –  Jun 02 '14 at 14:32
  • @Murplyx It was in German due to the link the answerer provided. I change the link to the English page. Cron jobs are common. If these links confuse you, then just Google about them. Tons of pages on them. – Giacomo1968 Jun 02 '14 at 14:33
  • where I put the time? –  Jun 02 '14 at 14:33
0

create a php file /path/to/your/php/script.php

<?php

    $log_file = /path/to/your/log/file.txt;

    file_put_contents($log_file, '');

?>

now set cronjob on your server

0  0  *  *  *  php -f /path/to/your/php/script.php

and you are done...

EDIT: if you don't want to use cronjob, then you can use this method

create a file name clear_logs.php

<?php

    $_00hours = date('His');


    if($_00hours == '000000'){

        $log_file = '/path/to/your/log/file.txt';

        file_put_contents($log_file, '');

    }

?>

Now include this file in your website files

Note: Second method is not recommended as user must come to website at 00:00:00 so that the statements under if condition can be executed