3

In a PHP webpage, Im opening a file in write mode, reading and than deleting the first line and closing the file. (The file has 1000's of lines)

Now, what the problem is, if there are like 100 users connected to that page, all will be opening that file in write mode and than try to write it after deleting the first line. Will there be any deadlocks in this situation?

For your information, we are using Windows server with IIS server and PHP5.
Thanks in advance for your help.

2 Answers 2

6

Use flock to grant access to file only for one user at a time.

But don't forget release your file lock by fclose

Update. Consider this code:

<?php
$start = time();
echo 'Started at '.$start.'<br />';
$filename = 'D:\Kindle\books\Brenson_Teryaya_nevinnost__Avtobiografiya_66542.mobi';
$fp = fopen($filename, 'w+') or die('have no access to '.$filename);

if (flock($fp, LOCK_EX)) {
    echo 'File was locked at '.time().'. Granted exclusive access to write<br />';
}
else {
    echo 'File is locked by other user<br />';
}
sleep(3);
flock($fp, LOCK_UN);
echo 'File lock was released at '.time().'<br />';
fclose($fp);
$end = time();
echo 'Finished at '.$end.'<br />';
echo 'Proccessing time '.($end - $start).'<br />';

Run this code twice (it locks file for 3 seconds, so let's consider our manual script run as asynchronous). You will see something like this:

First instance:

  • File was locked at 1302788738. Granted exclusive access to write
  • File lock was released at 1302788741

Second:

  • File was locked at 1302788741. Granted exclusive access to write
  • File lock was released at 1302788744

Notice, that second instance waited for first to release file lock.

If it does not comply your requirements, well... try to invent other solution like: user can read file, then he edit one line and save it as temporary, other user saves his temporary file and so on and once you have all users released file lock, you compose new file as patch of all temporary files on each other (use save files mtime to define which file should stratify other one)... Something like this.... maybe... I'm not the expert in this kind of tasks, unfortunately - just my assumption on how you can get this done.

Sign up to request clarification or add additional context in comments.

4 Comments

But what will happen to the other user, if the file is locked by the previous user?
He won't be able to aquire lock until first one releases it. You need the writer lock (exclusive) LOCK_EX (you can read about it in some comments to the official documentation).
@Nemoden, What happens if you don't close? Does the lock automatically close at the end of the php script?
could you clarify, is it fopen (fopen($filename, 'w+')) or flock (flock($fp, LOCK_EX)) call which will wait in the second script until the lock is removed? Is the if (flock($fp, LOCK_EX)) ... else { ... } used to check the problems with access to create a lock? The message in else is confusing, won't the script wait until the lock is removed anyway?
2

Use file locking, or a database that allows concurrent access. You will get in trouble otherwise.

2 Comments

But what will happen to the other user, if the file is locked by the previous user?
That depends on your implementation. You could display an error message or wait until the file is not locked anymore. Yes, it gets nasty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.