PHP文件锁定写入实例解析。
原文地址:http://www.jbxue.com/article/23118.html
PHP文件写入方法,以应对多线程写入,具体代码:
function file_write($file_name, $text, $mode='a', $timeout=30){ $handle = fopen($file_name, $mode); while($timeout>0){ if ( flock($handle, LOCK_EX) ) { // 排它性的锁定$timeout--; sleep(1); } } if ( $timeout > 0 ){ fwrite($handle, $text.'n'); flock($handle, LOCK_UN); fclose($handle); //释放锁定操作return true; } return false; }