Please write some PHP code to ensure that multiple processes simultaneously write to the same file successfully

Option One:

Copy the code
function writeData($filepath, $data) 
{ 
    $fp = fopen($filepath,'a'); do{ usleep(100); }while (!flock($fp, LOCK_EX)); $res = fwrite($fp, $data."\n"); flock($fp, LOCK_UN); fclose($fp); return $res; } 
Copy the code

This article comes from " I know ," blog, be sure to keep this source http://iknow.blog.51cto.com/6209466/1058876

Option II:

Copy the code
function writeData ( $ path, $ MODE, $ Data, $ MAX_RETRIES = 10 ) { $ FP = the fopen ( $ path, $ MODE ); $ retries = 0 ; do { IF ( $ retries> 0 ) { usleep ( RAND (. 1 , 10000 ));} $ + = retries. 1 ;} the while (! Flock ( $ FP, LOCK_EX) and $ retries <= $ MAX_RETRIES ); // determines whether or equal to the maximum number of retries, it returns a false iF ( $ retries == $ MAX_RETRIES ) { return to false ;} fwrite ( $ FP, " $ Data"); flock($fp, LOCK_UN); fclose($fp); return true; }
Copy the code

From http://www.xuejiehome.com/blread-1670.html

third solution:

Copy the code
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; }
Copy the code

 

flock

(PHP 4, PHP 5, PHP 7)

flock -  Portable advisory file locking

bool flock ( resource $handle , int $operation [, int &$wouldblock ] )

flock () allows to perform read / write models (including most Unix derivative version and even Windows) can use a simple on any platform.

Prior to PHP 5.3.2 version, the lock will be  fclose ()  release (called automatically at the end of the script).

PHP support in an advisory way (which means all accessing programs have to use the same locked, otherwise it will not work) to lock all files of a portable method. By default, this function will block to acquire the lock; this can be by the following documentation  LOCK_NB is controlled (on non-Windows platforms) option.

parameter

handle

File system pointer, is typically composed of  fopen ()  to create a  Resource (resource).

operation

operation It can be one of the following values:

  • LOCK_SHObtain a shared lock (read program).
  • LOCK_EX Obtain an exclusive lock (program written.
  • LOCK_UN Release latches (whether shared or exclusive).

If you do not want  flock () congestion in the locked, it is  LOCK_NB(not supported on Windows).

wouldblock

If the lock would block (EWOULDBLOCK the error code), the optional third parameter is set  TRUE. (Windows is not supported)

return value

Return on success  TRUE, or returns on failure  FALSE.

#. 1 example  Flock () Examples

Copy the code
<?php

$fp = fopen("/tmp/lock.txt", "r+");

if (flock($fp, LOCK_EX)) { // 进行排它型锁定 ftruncate($fp, 0); // truncate file fwrite($fp, "Write something here\n"); fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // 释放锁定 } else { echo "Couldn't get the lock!"; } fclose($fp); ?>
Copy the code

# 2 Example  Flock () using the  LOCK_NB options

Copy the code
<?php
$fp = fopen('/tmp/lock.txt', 'r+');

/* Activate the LOCK_NB option on an LOCK_EX operation */ if(!flock($fp, LOCK_EX | LOCK_NB)) { echo 'Unable to obtain lock'; exit(-1); } /* ... */ fclose($fp); ?>
Copy the code

Note:

Because  flock () requires a file pointer, and therefore may have to use a special lock file to protect access by going to write mode open file (in  fopen ()  function added "w" or "w +").

Warning:

In some operating systems  flock () to implement the process level. When using a multithreaded server API (such as ISAPI), you may not be able to rely on  flock () to protect files, because other parallel threads running PHP script can process the file to the same server instance.

flock () does not support the old file system, such as  FAT  and its derivates. Therefore, this environment will always return  FALSE(especially for Windows 98 users).

http://php.net/manual/zh/function.flock.php

Transfer: https: //www.cnblogs.com/gengyi/p/6399206.html

Guess you like

Origin www.cnblogs.com/jokmangood/p/11705894.html