Home >Backend Development >PHP Tutorial >PHP file locking ensures multi-threaded writing safety
The content of this article is about PHP file locking to ensure the safety of multi-threaded writing. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
[php] view plain copy function write_file($filename, $content) { $lock = $filename . '.lck'; $write_length = 0; while(true) { if( file_exists($lock) ) { usleep(100); } else { touch($lock); $write_length = file_put_contents($filename, $content, FILE_APPEND); break; } } if( file_exists($lock) ) { unlink($lock); } return $write_length; }
Related recommendations:
Error type of php file upload error
The above is the detailed content of PHP file locking ensures multi-threaded writing safety. For more information, please follow other related articles on the PHP Chinese website!