About using flock() in PHP

The flock function may have a problem caused by fopen modes.

504 views
d

By. Jacob

Edited: 2020-06-20 10:42

This is just a short warning about the use of flock for file locking. Using flock might not be the best option due to limited portability, and unexpected issues when using "w" with fopen—as mentioned in the official PHP documentation on flock:

note:
Because flock() requires a file pointer, you may have to use a special lock file to protect access to a file that you intend to truncate by opening it in write mode (with a "w" or "w+" argument to fopen()).

Contrary to what you should think, file-locking is far from straight forward; and considering critical systems might rely on it, you might not want to try and figure out all the points of failure on your own. If you can, use a database, as that will handle these things automatically. Alternatively, try to find a library that handles it for you.

Note. I have been working on a PHP file handler for some time now.

Background

Recently I learned that there may be a problem with flock when using the fopen with the "w" mode while writing files; possibly because this will delete the original file, resulting in destroying the original file pointer?

I have not tested this personally yet, and I am too busy right now. But, I have been working on several improvements to my class, and I hope to push them to GitHub soon, including a fix for this problem—if I find it effects my file handler class

Solution: using mkdir instead of flock?

A possible solution could be to use mkdir instead of flock.

Instead of using flock, I have seen some examples of using mkdir to create a lock file; this works by relying on mkdir's return state to tell if a lock (file) exists—this should also be atomic.

Using mkdir might be the best solution right now, since there is just too many variables to account for with flock. It is not just with using "w", but also differences between systems; I also currently do not account for differences between systems in my own file handler, but it seems mkdir is better for cross-platform compatibility with older systems.

Some of the issues that might arise are very rare, and only happen under very specific circumstances.

Again, this was just a short warning and reminder to be careful when you need to use file locking in your code.

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials