PHP implements two scenarios in which multiple processes simultaneously write to the same file share

Source: Internet
Author: User
Tags flock usleep
Test Centers: Understanding of functions

fopen: Open file or URL

Parameter 1: File address or URL  parameter The 2:mode parameter specifies the type of access required to the stream  ' r ' read-only open, pointing the file pointer to the file header.  the ' r+ '    read-write mode opens, pointing the file pointer to the file header. The  ' W ' Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it. The  ' w+ '    read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.  ' A ' writes open, pointing the file pointer at the end of the file. If the file does not exist, try to create it.  the ' A + '    read-write mode opens, pointing the file pointer at the end of the file. If the file does not exist, try to create it.

Fwrite: Writing Files

Parameter 1: FileName file system pointer  parameter 2: Content to write String type  return value: Write successfully returns the number of characters written, failure returns false

Flock: Lightweight consultation file lock

Parameter 1: File system pointer, is a resource (resource) typically created by fopen ().      Parameter 2: Mode  Lock_sh Get a shared lock (read program).  LOCK_EX obtains an exclusive lock (written program.  Lock_un Release Lock (whether shared or exclusive).  If you do not want flock () to block when locked, it is LOCK_NB (not yet supported on Windows).    return value: Returns TRUE on success, or FALSE on failure.

Fclose: Close an open file pointer

Parameter 1: File name  parameter 2: Returns TRUE on success, or FALSE on failure.

Programme I

function WriteData ($filepath, $data)   {       $fp = fopen ($filepath, ' a ');        do{           Usleep (+);       } while (!flock ($FP, LOCK_EX));  The LOCK_EX acquires an exclusive lock (written program) that locks the lock to write, the lock is      $res = fwrite ($fp, $data. " \ n ");       Flock ($FP, lock_un);    Lock_un release Lock (whether shared or exclusive).      fclose ($fp);        return $res;   }

Scenario two (self-tagging method)

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.