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

Source: Internet
Author: User

  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+ ');  

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.