Home  >  Article  >  Backend Development  >  PHP high concurrent access to write files

PHP high concurrent access to write files

WBOY
WBOYOriginal
2016-07-25 08:46:081141browse
  1. function write ( $data )
  2. {
  3. $return = false;
  4. if ( $handle = @fopen ( 'error.txt','a+' ) )
  5. {
  6. $i=0;
  7. while ( !flock ( $handle,2 ) && $i++ < 3 )
  8. {
  9. usleep ( 500000 );
  10. }
  11. if ( $i < 3 && fwrite ( $handle,$data . "rn" ) )
  12. {
  13. flock ( $handle,3 );
  14. $return = true;
  15. }
  16. fclose ( $handle );
  17. }
  18. return $return;
  19. }
复制代码

php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn