PHP uses multiple processes to simultaneously control file read and write samples

Source: Internet
Author: User
Tags flock rand

  This article mainly introduces PHP to use multiple processes to control file read and write examples, need friends can refer to the following

The code is as follows: <?php/**  * Write Data  * @param  [string] $path [File path]  * @param  [string] $mode [File open mode] & nbsp;* @param  [string] $data [data]  * @return [bool]         */function WriteData ($pat H, $mode, $data) {        $fp = fopen ($path, $mode);         $retries = 0;   &N Bsp     $max _retries = 100;        do {       -($retries > 0) {         uslee P (rand (1, 10000));        }         $retries + 1;        }while (!flock ($FP, LOCK_EX) and $retries <= $max _retries);        if ($retries = = $max _retries) {        return false;       &NBSP}        fwrite ($fp, $data. " RN ");        flock ($fp, Lock_un);        fclose ($FP);       &Nbsp;return true;    /**  * read data  * @param  [string] $path [File path]  * @param  [string] $mode [File open mode] &n bsp;* @return string        */function ReadData ($path, $mode) {      $fp = fopen ($path, $mode);       $retries = 0;       $max _retries = 100;      do {      if ($retries > 0) {       usleep (rand (1, 10000)); &N Bsp    }       $retries + 1;      }while (!flock ($FP, lock_sh) and $retries <= $max _retries);      if ($retries = = $max _retries) {      return false;      }       $contents = "";      while (!feof ($fp)) {        $contents. = Fread ($fp, 8192);            flock ($fp, Lock_un);      fclose ($FP);      return $contents; }   writedATA (' D:/webserver/demo.txt ', ' A + ', ' is a demo '); echo readdata (' D:/webserver ', ' r+ ');  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.