简体   繁体   中英

How to operate txt file on PHP?

I have txt file, which keeps 1 as in the following query to take the contents from txt and +1

 function analitic(){

    $file = fopen(TEMPLATEPATH . "/analitic.txt", "w");
    $txt = $_POST['data']; //$txt = 1                           
    fwrite($file, $txt);
    fclose($file);

 }

And how to add a date in the txt file? And if the date is different, save the data in the next line?

Thank you google translate :)

This will work...

file_put_contents(TEMPLATEPATH."/analitic.txt",PHP_EOL.date('c').PHP_EOL,FILE_APPEND);
  • PHP_EOL - this the "next line" character
  • file_put_contents / FILE_APPEND is a slightly more concise method of writing to a file
  • 'c' - is the date format for an ISO date time, please see http://php.net/manual/en/function.date.php for more information

Some think like this:

function my_action_myFunc(){
    $data = $_POST['data'];
    $date = date('d.m.Y');
    $file = file_get_contents(TEMPLATEPATH."/analitic.txt",null,null, 10);
    settype($file, "integer");
    settype($data, "integer");
    $a = $file+$data;
    file_put_contents(TEMPLATEPATH."/analitic.txt",$date.PHP_EOL.$a);
    wp_die();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM