How to lock a file using PHP
In this post, you will learn how to lock a file using the PHP programming language.
File locking is an important task. It restricts other users from changing a specific file. It allows only one user at a time to access or modify it. PHP provides the flock() method for portable advisory file locking. This was introduced with PHP version 4.0. This method provides options to mark the file for locking purposes. There are two kinds of locks we can make with flock- shared locks and exclusive locks. If you want to read only lock the file, use the shared lock, and if you want to lock the file for writing purposes, use the exclusive lock.
Syntax of flock()
flock($file_handler, $operation, $block)
$file_handler- It is a file handler pointer created by using fopen().
$operation- The lock operation can contain one of the following constants-
- LOCK_SH- It requests a shared lock (reader).
- LOCK_EX- It requests an exclusive lock (writer).
- LOCK_UN- It releases a lock either shared or exclusive.
- LOCK_NB- It avoids blocking other processes while locking.
The $block is optional, set to 1 to block other processes while locking. The flock() function returns a boolean value. It returns TRUE if the file lock is retrieved successfully and FALSE otherwise.

PHP flock() Exclusive lock (LOCK_EX) example
<?php
$php_errormsg = 'Error to lock a file.';
$write = 'Natural environment plays a great role in the existence of life on earth.';
$fh = fopen('test.txt','a') or die($php_errormsg);
if( flock($fh,LOCK_EX) ) {
fwrite($fh,$write) or die($php_errormsg);
fflush($fh) or die($php_errormsg);
flock($fh,LOCK_UN);
}
fclose($fh) or die($php_errormsg);
?>
In the above code, we have opened a file 'text.txt' and assigned the file handler to $fh. Next, we lock the file using LOCK_EX (exclusive lock) in flock() function and write the $write variable to the file. The fflush() function flushes the output to the file. Finally, we released the locked file using LOCK_UN in the flock() function and closed the file handler. If you are using PHP version >= 5.3, you must manually unlock the file using the fclose() function.
When you execute the above code, it will append the given content at the end of the text file. Make sure to provide the write file path. If any error occurs during script execution, it will return the value provided in '$php_errormsg'.
PHP flock() Shared lock (LOCK_SH) example
As you saw in the above example, we have a locked file for writing. Here you will know how to read a locked file.
<?php
$php_errormsg = 'Error to lock a file.';
$fh = fopen('test.txt','r+') or die($php_errormsg);
if( flock($fh,LOCK_SH) ) {
$content = fread($fh, 1000);
echo $content;
fclose($content);
}
?>
PHP flock() Non-blocking locking example
When the file is locked by another user, and you don't want to block while locking, use LOCK_NB as a bitmask to LOCK_SH or LOCK_EX.
<?php
$php_errormsg = 'Error to lock a file.';
$fh = fopen('test.txt','w+') or die($php_errormsg);
$wouldblock = null;
if(flock($fh, LOCK_EX | LOCK_NB)) {
fwrite($fh,'Write content here') or die($php_errormsg);
fflush($fh) or die($php_errormsg);
flock($fh,LOCK_UN);
fclose($content);
}
else {
echo 'File is currently locked by other user';
}
?>
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
How to upload multiple files and store in MySQL database using PHP
PHP code to generate Captcha and add in contact form with Validation
PHP File Upload MIME Type Validation with Error Handler
File Upload Validation in PHP
jquery upload file progress bar
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
Simple pagination in PHP
Simple PHP File Cache