Implementing File Locking in PHP


Prevent simultaneous access to a file by multiple scripts.

Source Code

$file = fopen("test.txt", "a+");
if (flock($file, LOCK_EX)) { // Exclusive lock
    fwrite($file, "New contentn");
    flock($file, LOCK_UN); // Release the lock
} else {
    echo "Could not lock the file!";
}
fclose($file);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments