Why does it sometimes return an empty string file_get_contents or fgets ?
In this case, we will consider the function fgets .
It was tested with the http and https protocol, the same thing happens. The truth, it seemed, with http works more stable, but anyway, sooner or later it returns an empty string. Checked without the .htaccess file to exclude additional options for the occurrence of errors.
To cause an error , you need to go to the address of this page, the code that I post, several times in a row, changing the value of the number variable in the GET request.
Here is an example :
Step 1 . http: //myhost/1/index.php? number
Step 2 . http: //myhost/1/index.php? number
Step 3 . Here at this step, fgets can already produce an empty string, but if it did not produce an empty string, try further changing the variable in the GET request http: //myhost/1/index.php? Number = 194
Step 4 . http: //myhost/1/index.php? number = 19
Step 5 . http: //myhost/1/index.php? number = 1
Step 6 . http: //myhost/1/index.php? number =
Step 7 . http: //myhost/1/index.php? number
Step 8 . http: //myhost/1/index.php? number = 195
.... etc
When fgets returns an empty string, fopen at that time gives it away -
resource(199) of type (stream) , that is, apparently, this is probably normal for fopen , but until the end I'm not sure.
Cat code :
<?php $a2 = $_GET['number']; if($_SERVER['REQUEST_METHOD'] == 'GET'){ for ($i=0; $i < 1000; $i++) { //Проверяем, есть ли этот файл if(file_exists(__DIR__.'/cahe.json')){ $fp = fopen(__DIR__.'/cahe.json', "r" ); if($fp == false || $fp == ''){ return var_dump("============= ОШИБКА =============="); } $mytext = '-'; while (!feof($fp)) { $mytext = fgets($fp, 999); } if($mytext == ''){ var_dump($mytext); return var_dump($fp); } $json_data = json_decode($mytext, true); fclose($fp); $c = $json_data['count_m'] + 1; $data = '{"count":"1", "count_m":"'.$c.'"}'; $fp = fopen(__DIR__.'/cahe.json', "w" ); fwrite($fp, $data); fclose($fp); }else{ return var_dump("============= ФАЙЛА НЕТУ =============="); } } } Note * If, go directly to this page, without passing a variable in a GET request
And , remove this piece of code:
$a2 = $_GET['number']; if($_SERVER['REQUEST_METHOD'] == 'GET'){} Then everything works stably ( DOES NOT WORK STABLE, ENTRIES TO FILE AS HIT, JUST A BLANK LINE IS NOT DRAWN ).
Here is code2 (also unstable)
for ($i=0; $i < 1000; $i++) { $data = file_get_contents(__DIR__.'/cahe.json'); $json_data = json_decode($data, true); if($data == false || $data == ''){ return var_dump($data.' - '.$i.' $number-'.$number); }else{ $count_index = $json_data['count_index']; $count_index = $count_index + 1; $data = '{"count":"1", "count_index":"'.$count_index.'"}'; // Флаг LOCK_EX для предотвращения записи данного файла кем-нибудь другим в данное время file_put_contents(__DIR__.'/cahe.json', $data, LOCK_EX); } }
'{"count":"1", "count_m":"'.$c.'"}';You read everything correctly, the variablenumbernot used. It just stands there for research, in order to show this bug or error. - gilo1212