How to read the information and after, write it to a text file?

I try to do it as described in the following text, but apparently, a race condition is formed . Suppose if there are only two iterations in the loop, then this code will work well.

See:

File cahe.json is in the system and contains the original entry

{"count":"1", "count_1":"0"} 

We use variant A with two iterations for ($i=0; $i < 2; $i++) {

at the output we get a record in the file

 {"count":"1", "count_1":"2"} 

We use variant B with a thousand iterations for ($i=0; $i < 1000; $i++) {

at the output we get a record in the file

 {"count":"1", "count_1":"01"} 

instead

 {"count":"1", "count_1":"1000"} 

We use the C variant with a thousand iterations for ($i=0; $i < 1000; $i++) { , but we change the initial value in the cahe.json file

with

 {"count":"1", "count_1":"0"} 

on

 {"count":"1", "count_1":"1000"} 

at the output we get a record in the file

 {"count":"1", "count_1":"2000"} 

Use option D with a thousand iterations for ($i=0; $i < 1000; $i++) { and plus , remove a piece of code filesize(__DIR__.'/cahe.json') , replacing it with this one - '20000', let's say

at the output we get a record in the file

 {"count":"1", "count_1":"1000"} 

//CODE

 for ($i=0; $i < 1000; $i++) { //------------------------------------------------ $json_data; $contents = ''; $fp = fopen(__DIR__.'/cahe.json', "c+" ); if($fp == false){ //Π±Ρ‹Π»Π° ошибка var_dump("Π‘Ρ‹Π»Π° ошибка ΠΏΡ€ΠΈ ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚ΠΈΠΈ Ρ„Π°ΠΉΠ»Π°"); }else{ if (flock($fp, LOCK_EX)) { // выполняСм ΡΠΊΡΠΊΠ»ΡŽΠ·ΠΈΠ²Π½ΡƒΡŽ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΡƒ //------------------------------------------------ $contents = fread($fp, filesize(__DIR__.'/cahe.json')); $json_data = json_decode($contents, true); //------------------------------------------------ ftruncate($fp, 0); // ΠΎΡ‡ΠΈΡ‰Π°Π΅ΠΌ Ρ„Π°ΠΉΠ» //ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π°Π΅ΠΌ Ρ„Π°ΠΉΠ»ΠΎΠ²Ρ‹ΠΉ ΡƒΠΊΠ°Π·Π°Ρ‚Ρ‚Π΅Π»ΡŒ Π² Π½Π°Ρ‡Π°Π»ΠΎ Ρ„Π°ΠΉΠ»Π° fseek($fp, 0); //------------------------------------------------ $count_1 = $json_data['count_1'] + 1; $data = '{"count":"1", "count_1":"'.$count_1.'"}'; fwrite($fp, $data); //------------------------------------------------ fflush($fp); // ΠΎΡ‡ΠΈΡ‰Π°Π΅ΠΌ Π²Ρ‹Π²ΠΎΠ΄ ΠΏΠ΅Ρ€Π΅Π΄ ΠΎΡ‚ΠΌΠ΅Π½ΠΎΠΉ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠΈ flock($fp, LOCK_UN); // ΠΎΡ‚ΠΏΠΈΡ€Π°Π΅ΠΌ Ρ„Π°ΠΉΠ» } else { var_dump("НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΡƒ!"); } fclose($fp); } //------------------------------------------------ } 
  • @ Jean-Claude And how to do it is not stupid? Is there any other options? - gilo1212
  • Outside the cycle opened, at the end of the cycle closed. Only 1 time. Or file_get_contents () and file_put_contents (), 1 time each. - Visman
  • It does not work, files can be different. This time. Two, with file_get_contents() due to file_get_contents() , competing streams are formed and there will be partial data loss at the output. - gilo1212
  • @ Jean-Claude can be written and text simple. What is the difference? There all the same json is stored and not just text. - gilo1212
  • Describe the task in more detail. It’s completely unclear why you need to use the file system. Where did competing streams come from? What do you need to get in total? You obviously took the wrong decision for your task - ArchDemon

1 answer 1

incorrectly plows filesize () for you, when the digit is up to 9, reads 28 bytes, when written down 10, reads again 28 characters, not 29, so json cannot read the data and resets it.

At the docks http://php.net/manual/ru/function.filesize.php

Note: The results of this function are cached. For more information, see the clearstatcache () section.

To clear the cache, the clearstatcache () function must be executed.