Solution of file_put_contents concurrency problem

Source: Internet
Author: User


Solution One , file_put_contents high concurrency and exclusive locking

Data is empty when high concurrent access is found using File_put_contents to write files.

View Official documents:

int file_put_contents (string $filename, String $data [, int $flags [, Resource $context]])
Parameters:

FileName to be written to the data file name. Data to write to. Types can be String,array or stream resources (as described above). Flags flags can be file_use_include_path,file_append and/or LOCK_EX (get an exclusive lock), but use File_use_include_path with special care. Context a context resource.
Direct until the flags parameter is LOCK_EX to obtain an exclusive lock at high concurrency.

In addition, the Flock function also provides a file locking method:

$fp = fopen ("/tmp/lock.txt", "w+");
if (Flock ($FP, LOCK_EX)) {//For exclusive locking of
fwrite ($fp, "Write something here\n");
Flock ($FP, lock_un); Release Lock
} else {
echo ' couldn ' t lock the file!
}
Fclose ($FP);

Note that flock () requires a file pointer.

method Two , file_put_contents () has a high concurrency problem, and the Smarty processing method is as follows.

<?php   Define ("File_put_contents_atomic_temp",  dirname (__file__).

/cache ");

Define ("File_put_contents_atomic_mode",  0777);   Function file_put_contents_atomic ($filename,  $content)  {      $

Temp = tempnam (file_put_contents_atomic_temp,  ' TEMP ');     if  (!) ( $f  =  @fopen ($temp,  ' WB '))  {         $temp  =

 file_put_contents_atomic_temp . directory_separator . uniqid (' TEMP ');         if  (!) ( $f  =  @fopen ($temp,  ' WB '))  {             trigger_error ("File_put_contents_atomic ()  : error writing temporary file

  ' $temp ',  e_user_warning);

            return false;    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP}    &nbsp}       fwrite ($f,  $content

);

    fclose ($f);       if  (! @rename ($temp,  $filename))  {     

    @unlink ($filename);

         @rename ($temp,  $filename);

&NBSP;&NBSP;&NBSP;&NBSP}        @chmod ($filename,  file_put_contents_atomic_mode);

      return true;  }  ?>

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.