Solution One , file_put_contents high concurrency and exclusive locking
Data is empty when high concurrent access is found using File_put_contents to write files.
View Official documents:
int file_put_contents (string $filename, String $data [, int $flags [, Resource $context]])
Parameters:
FileName to be written to the data file name. Data to write to. Types can be String,array or stream resources (as described above). Flags flags can be file_use_include_path,file_append and/or LOCK_EX (get an exclusive lock), but use File_use_include_path with special care. Context a context resource.
Direct until the flags parameter is LOCK_EX to obtain an exclusive lock at high concurrency.
In addition, the Flock function also provides a file locking method:
$fp = fopen ("/tmp/lock.txt", "w+");
if (Flock ($FP, LOCK_EX)) {//For exclusive locking of
fwrite ($fp, "Write something here\n");
Flock ($FP, lock_un); Release Lock
} else {
echo ' couldn ' t lock the file!
}
Fclose ($FP);
Note that flock () requires a file pointer.
method Two , file_put_contents () has a high concurrency problem, and the Smarty processing method is as follows.
<?php Define ("File_put_contents_atomic_temp", dirname (__file__).
/cache ");
Define ("File_put_contents_atomic_mode", 0777); Function file_put_contents_atomic ($filename, $content) { $
Temp = tempnam (file_put_contents_atomic_temp, ' TEMP '); if (!) ( $f = @fopen ($temp, ' WB ')) { $temp =
file_put_contents_atomic_temp . directory_separator . uniqid (' TEMP '); if (!) ( $f = @fopen ($temp, ' WB ')) { trigger_error ("File_put_contents_atomic () : error writing temporary file
' $temp ', e_user_warning);
return false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP}  } fwrite ($f, $content
);
fclose ($f); if (! @rename ($temp, $filename)) {
@unlink ($filename);
@rename ($temp, $filename);
&NBSP;&NBSP;&NBSP;&NBSP} @chmod ($filename, file_put_contents_atomic_mode);
return true; } ?>