Test Centers: Understanding of functions
fopen: Open file or URL
Parameter 1: File address or URL parameter The 2:mode parameter specifies the type of access required to the stream ' r ' read-only open, pointing the file pointer to the file header. the ' r+ ' read-write mode opens, pointing the file pointer to the file header. The ' W ' Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it. The ' w+ ' read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it. ' A ' writes open, pointing the file pointer at the end of the file. If the file does not exist, try to create it. the ' A + ' read-write mode opens, pointing the file pointer at the end of the file. If the file does not exist, try to create it.
Fwrite: Writing Files
Parameter 1: FileName file system pointer parameter 2: Content to write String type return value: Write successfully returns the number of characters written, failure returns false
Flock: Lightweight consultation file lock
Parameter 1: File system pointer, is a resource (resource) typically created by fopen (). Parameter 2: Mode Lock_sh Get a shared lock (read program). LOCK_EX obtains an exclusive lock (written program. Lock_un Release Lock (whether shared or exclusive). If you do not want flock () to block when locked, it is LOCK_NB (not yet supported on Windows). return value: Returns TRUE on success, or FALSE on failure.
Fclose: Close an open file pointer
Parameter 1: File name parameter 2: Returns TRUE on success, or FALSE on failure.
Programme I
function WriteData ($filepath, $data) { $fp = fopen ($filepath, ' a '); do{ Usleep (+); } while (!flock ($FP, LOCK_EX)); The LOCK_EX acquires an exclusive lock (written program) that locks the lock to write, the lock is $res = fwrite ($fp, $data. " \ n "); Flock ($FP, lock_un); Lock_un release Lock (whether shared or exclusive). fclose ($fp); return $res; }
Scenario two (self-tagging 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; }