Two methods for PHP to write multiple processes to the same file at the same time

Source: Internet
Author: User
PHP provides two solutions for writing multiple processes to the same file at the same time. test site: Understanding of functions fopen: open a file or URL test site: Understanding of functions

Fopen: open a file or URL

Parameter 1: file address or URL parameter 2: the mode parameter specifies the required stream access type 'R' to open in read-only mode and points the file pointer to the file header. Open in 'R + 'read/write mode and point the file pointer to the file header. Open in 'W' writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. Open in 'W + 'read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. Open the 'A' write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. Open in 'A + 'read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.

Fwrite: write files

Parameter 1: file name file system pointer parameter 2: string type returned value of the content to be written: the number of characters written after successful write, and false after failure

Flock: lightweight consultation file locking

Parameter 1: file system pointer, which is a resource typically created by fopen ). Parameter 2: The mode LOCK_SH gets the shared Lock (read program ). LOCK_EX gets an exclusive lock (written program. LOCK_UN releases the lock (whether shared or exclusive ). If you do not want flock () to be blocked during the lock, it is LOCK_NB (not supported on Windows ). Return value: TRUE is returned when the request is successful, or FALSE is returned when the request fails.

Fclose: close an open file pointer.

Parameter 1: file name parameter 2: TRUE is returned if the call succeeds or FALSE is returned if the call fails.

Solution 1

Function writeData ($ filepath, $ data) {$ fp = fopen ($ filepath, 'A'); do {usleep (100);} while (! Flock ($ fp, LOCK_EX); // LOCK_EX gets an exclusive lock (the program written to) to sort the lock to get the lock and write the lock, $ res = fwrite ($ fp, $ data. "\ n"); flock ($ fp, LOCK_UN); // LOCK_UN release lock (whether shared or exclusive ). Fclose ($ fp); return $ res ;}

Solution 2 (self-marking 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;  }

The above are the details shared by the two solutions for PHP to write multiple processes to the same file at the same time. For more information, see other related articles in the first PHP community!

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.