0

I am working on a PHP web application, my first web application. One of the processes needs to copy some text files to a destination directory. In the destination directory it will open the text files and show the output in the web browser. I am wondering what happens if two users at the same time initiate the process? Since the files MUST be copied to destination directory and read from there. Do I create a unique destination directory each time? How is this properly done?

1 Answer 1

1

You could create a unique directory, it all depends on what your goal is. Do you want to allow two different users to execute it at the same time?

Databases are usually useful in these situations because they are made for concurrent activity and feature table or even row locking.

In this case if you wanted to prevent a second user from running the process at the same time, you could use a lock file:

  1. Create a lock file if it doesn't exist
  2. Run the processes
  3. Unlink the lock file

If the lock file exists, wait until it is unlinked (deleted), an example:

while (file_exists('file.lock')) {
   usleep(100000); // sleep 100ms
}
touch('file.lock');
// Execute processes here
unlink('file.lock');

Lock files are commonly used in filesystems and applications to prevent users from modifying a file at the same time or running more than one instance.

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

2 Comments

Yes I would like to allow two different users, or more, to execute it at the same time. Instead of locking the users, i would like them to be able to run the process at any time. So, there is nothing wrong by creating unique directories?
I don't see anything wrong with it, of course I have little idea of what you are doing. I would recommend containing them in a directory like process/sessionid and then removing sessionid when you are done for cleanup (unless you plan on reusing them later)

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.