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'; >?>
File locking in php
Если наш веб-сайт посещает множество людей, которые одновременно обращаются к одному и тому же файлу, то мы можем столкнуться с некоторыми проблемами. В частности, при одновременной попытке записи несколькими пользователями файл может быть поврежден, либо выдать неожиданные результаты, если один человек считывает файл, а другой одновременно записывает его. Возникает проблема синхронизации доступа пользователей.
Чтобы ограничить доступ к файлу, в PHP используется функция flock() . Эта функция блокирует доступ к файлу, когда он уже занят одним пользователем, а все остальные запросы ставит в очередь. При освобождении файла он разблокируется, передается первому в очереди пользователю и снова блокируется.
Функция имеет следующее определение:
bool flock (resource $handle , int $operation [, int &$wouldblock ])
Первый параметр — дескриптор файла, возвращаемые функцией fopen() .
Второй параметр указывает на тип блокировки. Он может принимать следующие значения:
- LOCK_SH (или число 1): разделяемая блокировка (чтение файла)
- LOCK_EX (или число 2): исключительная блокировка (запись файла)
- LOCK_UN (или число 3): для снятия блокировки
- LOCK_NB (или число 4): эта константа используется только вместе с одной из предыдущих в битовой маске (LOCK_EX | LOCK_NB), если не надо ждать пока flock() получит блокировку
Третий необязательный параметр $wouldblock будет содержать true , если блокировка будет блокирующей.
При успешном выполнении функция flock возвратит значение true , а в случае ошибки — false .
Используем flock для ограничения доступа к файлу:
При изменении файла блокировка ставится непосредственно перед внесением изменений, а снимается сразу после их внесения. Иначе программа может замедлить свою работу. Поэтому вызов, блокирующий файл: flock($fd, LOCK_EX) поставлен непосредственно перед вызовом функции fwrite($fd, «$str») . А снятие блокировки с помощью константы LOCK_UN идет сразу после записи.
Обратите внимание, что при открытии файла здесь использовался режим ‘r+’, а не ‘w’ или ‘w+’, так как ‘w’ и ‘w+’ уже подразумевают изменение файла. Поэтому при блокировке, даже если надо делать запись, не рекомендуется использование ‘w’ и ‘w+’.
Если нам надо стереть все содержимое файла и перезаписать файл по-новому, то мы можем воспользоваться функцией ftruncate :
How to Lock File in PHP
While writing to files that are possibly being read by other scripts at the same time, you will run into problems at some point specifically when write is not totally be completed because another script is reading the same file. The reading script will only see a partial file at that moment.
To Prevent, this problem is not hard to do, and the method to prevent this problem is called locking. We can set locks on files with flock() function in PHP. If the file is locked with flock() function, it is prevented to read a script from reading a file when it is being written to by another script.
flock() allows the implementation of a simple platform that can be used for read/write any of the files. If the optional third argument is set to TRUE, the lock would block. Lock operation can release when call fclose() function called or code execution is completed.It will automatically release the lock. If successful it returns TRUE, on failure returns FALSE.
PHP support in a better way for locking so you have to use the common way to lock the file or it will not work. A portable method for locking all the files, You can use the flock () function. flock() function handle operation must be an open file pointer. the operation can be one of the following values:
Second and important parameter operation is for three kinds of possible locks.
- Shared lock(Reader)
- exclusive lock(Writer) :LOCK_EX acquire an exclusive lock on the file and blocks until this lock can be acquired.An exclusive lock will only be granted if there are no other locks on the file.
- Un lock After we write to the file, we can release the lock with flock($fp,LOCK_UN);
Let’s have a look with LOCK_EX option in flock() function: