PHP realizes that multiple processes write to a file at the same time successfully

<?php
function writefile( $filename, $data ){

 $fp = fopen( $filepath, 'a'); // Open the file by appending and return the pointer
 do{

  usleep(100 );  // Pause execution Program, the parameter is in microseconds
 }while( !flock( $fp, LOCK_EX ) );
// Lock the file in exclusive write mode, return TRUE if successful , otherwise FALSE
}

 $res = fwrite( $fp , $data."/n"); //  Write data to the open file by appending
 flock( $fp, LOCK_UN );
// Unlock for other processes to lock
 fcloce( $fp );
// Close the open file pointer
 return $res;
// return write result
?>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325386054&siteId=291194637