There is a text file in which to write the following:

Post comes $_POST['data'] Post = 1 I save this value in a text file, for example, the next post $_POST['data'] comes in which has the same value. And I need to take a unit from a text file, add the one that came to it and overwrite it in a file, that is, we save it to a file 2. During the day, there may be about 500 such posts.

Further, I would like to attribute the date of today, for example, the next day, the record goes on the next line. Is it hard to do?

I had enough of this:

 $file = fopen(TEMPLATEPATH . "/analitic.txt", "w"); $txt = $_POST['data']; //$txt = 1 fwrite($file, $txt); fclose($file); 
  • Have you thought about what will happen if you run such a script at the same time and at the same time someone will read from that file and someone else will write? .... I’ll say nothing good at all))) - Lesiuk Alexey
  • Nobody will write anything there) units are simply summed up there, chip = how many times a button was pressed a day, when clicked, one unit comes, that's all) - Alexander Reizan

2 answers 2

I did not check for performance, but somehow:

 $filename = "TEMPLATEPATH . "/analitic.txt""; $num = $_POST['data']; if (file_exists($filename)) { $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); fclose($handle); $num = $num + (int)$contents; } $file = fopen($filename, "w"); fwrite($file, $num); fclose($file); 

    I did this:

     function my_action_myFunc(){ $data = $_POST['data']; $date = date('dmY'); $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(); }