How to delete TXT file content using PHP?

-4

I'm doing a web project in php to monitor with ping the network assets in my work I have a file named 'log.txt' in the root of my project that is fed with log information with date, time, ip etc. Line by line. Here's an example:

function functionLog($vSetor, $vIp, $vTaxa, $vPorta) {
    $fp = fopen("log.txt", "a");
    $dataLog = date("d/m/Y");
    date_default_timezone_set('America/Belem');
    $horaLog = date("H:i:s");
    fwrite($fp, $dataLog . ' ' . $horaLog . ' ' . $vSetor . ' ' . $vIp . ' ' . $vTaxa . 'ms' . ' ' . $vPorta . "\r\n");
    fclose($fp);
}
?>

But there comes a time when this file is too full of log information and need to empty with just one click

My question is: how can I call a function inside an html button so that the file is zeroed, that is, all the lines in the 'log.txt' file are deleted

    
asked by anonymous 27.12.2018 / 20:13

1 answer

0

You have another alternative.

You could invoke the function using the button on a form.

And for it to look cool, you could style it with css.

For example:

HTML Form

<form action="/urlaqui" method="post">
    <button type="submit" name="botao">Invocar Função</button>
</form>

Invoke PHP function after clicking the

button
<?php
    // Verifica se o botão foi clicado
    if( isset($_POST['botao']) ){
       // Se o botão existe, executa a função
       executeEstaFuncao();
    }

Styling button with CSS

form button{
    background-color:#ccc;
    border:1px solid #444;
    color:#666;
    cursor:pointer;
    font-size:1.1em;
}
    
30.12.2018 / 14:45