Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7844033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:45:12+00:00 2026-06-02T16:45:12+00:00

Ok, I know that there’s been similar questions on this site about this problem,

  • 0

Ok, I know that there’s been similar questions on this site about this problem, but none of this questions and provided answers isn’t exactly what I need.

I’m building flat-file based CMS.
What if, for example:

  1. 2, 3, 10….. fwrite in appending mode requestes come to same php file “contact_form_messages_container.php” at the same time?

  2. 2, 3, 10….. fwrite in “w” mode requestes come to same php file which holds the simpley nubmer of specific page visits, again at the same time?

I know about flock() function, but it could happen two or more flock() requests comes on the same time… Does anyone knows solution to this problem? Only thing I have on my mind is usleep()-ing the script using while looop for some amount of time, until the target file becomes availibile, but I do not have idea if it works, where and how to perform this?
Does anyone have practical expirience with this issue?
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:45:14+00:00Added an answer on June 2, 2026 at 4:45 pm

    The flock() function is designed to handle multiple concurrent readers and writers for file operations; by default flock() may suspend a process until a compatible lock can be obtained (i.e. shared or exclusive). Once obtained, a lock can later be released to allow other processes to operate on the file; locks are released implicitly when the file is closed or the process ends.

    Unless your files are on NFS, I highly doubt you will ever run into a situation whereby two conflicting locks would be given simultaneously.


    The following illustrates a basic example of using flock():

    // open the file (take care to not use "w" mode)
    $f = fopen('file.txt', 'r+');
    // obtain an exlusive lock (may suspend the process)
    if (flock($f, LOCK_EX)) {
        // this process now holds the only exclusive lock
    
        // make changes to the file
    
        // release the lock
        flock($f, LOCK_UN);
    }
    // don't perform any write operation on $f here
    fclose($f);
    

    Using the LOCK_NB flag together with LOCK_EX or LOCK_SH will prevent the process from being suspended; if the call returns false a third parameter can be passed to determine whether the process would have been suspended (not supported on Windows).

    if (false === flock($f, LOCK_EX | LOCK_NB, $wouldblock)) {
        if ($wouldblock) {
            // the lock could not be obtained without suspending the process
        } else {
            // the lock could not be obtained due to an error
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there have been many similar questions on this topic but none
I know that there's quite a few subjects similar to this one, but none
I know that there are many questions about this but I cannot make it
I know that there are more than a dozen questions about this. But I
I know that there are many similar questions posted, but none of them refers
I know that there has been one question about this but it is not
I know that there are plenty of similar questions on stack overflow. But the
I know that there are similar questions, but I still can not resolve it.
I know that there are some questions about this already, most relate to either
I know that there have been plenty of topics describing this topic but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.