<? PHP $ filepath = "wfile.txt"; echo dirname ($ filepath ). "<br/>"; // permission check is_writeable (dirname ($ filepath) or die ("can not write $ filepath <br/>"); is_readable ($ filepath) or die ("can not read $ filepath <br/>"); // open the file $ fhandle = fopen ($ filepath, "W + B "); // write data $ I = 0; If (flock ($ fhandle, lock_ex | lock_nb) // Add the write lock {fwrite ($ fhandle, "content1 "); fwrite ($ fhandle, "content2 \ r \ n"); fwrite ($ fhandle, "content3"); usleep (1000000); e Cho $ I. "<br/>"; $ I ++; if ($ I = 20) break;} else {echo "can not lock ". $ filepath. "For write <br/>";} // else // read data if (flock ($ fhandle, lock_sh) // read the lock {fseek ($ fhandle, 0 ); while (! Feof ($ fhandle) {$ STR = fgets ($ fhandle); echo $ Str. "<br/>" ;}} else {echo "can not lock ". $ filepath. "For read <br/>" ;}// output: content1content2 | content3 // read the element $ arr = file ($ filepath) in the array ); var_dump ($ ARR );