Back to Top

How to Lock File in PHP

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.

SYNTAX:

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.

  1. Shared lock(Reader)
  2. 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.
  3. 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:

Here, I have used while loop which just the condition you want to, It is just a true here, but you can change as per your requirement. fopen() function will read the lockfile.txt in write mode and next, I pass the file pointer #fp to the flock function with parameter LOCK_EX which will lock the file as an exclusive lock.AS flock() function requires a file pointer, You need to use fopen() function or special file function which can return the file pointer.

You will also like to read the article, how you can create a temporary File in PHP which is useful to create the temporary file in PHP.flock () does not support the old file systems such as FAT. Therefore, it always returns FALSE for such files.fread() function efficiency is much higher than the fwrite() function efficiency in PHP

Let’s check one more code which shows LOCK_SH(a shared lock)

LOCK_SH lock will not be granted if there is an exclusive lock set on this file, but it will be granted if there is another shared lock or no lock at all on this file.This means there can be multiple readers reading the same file at the same time until a file can’t be written by another writer with exclusive lock(LOCK_EX).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Static Methods and Properties in PHP

Posted on 6 years ago

Bhumi

Getting started with Laravel

Posted on 1 year ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

ECMAScript 6 New Features – 2

Posted on 5 months ago

Bhumi

How to use MySQL TimeStamp datatype

Posted on 6 years ago

Bhumi