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 4322140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:50:08+00:00 2026-05-21T08:50:08+00:00

I need to lock the file, read the data, write to the file and

  • 0

I need to lock the file, read the data, write to the file and then close it. The problem that I have is I’m trying to find the correct mode for fopen.

With ‘a+’ – always append data, with ‘w+’ truncated all data when open, with ‘x+’ – fails to lock the file.

This is my code:

$fh_task = fopen($task_file, 'w+');
flock($fh_task, LOCK_EX) or die('Cant lock '.$task_file);
$opt_line = '';
while(!feof($fh_task)){
  $opt_line .= fread($fh_task, 4096);
}
$options = unserialize($opt_line);
$options['procceed']++;
rewind($fh_task);
fwrite($fh_task, serialize($options));
flock($fh_task, LOCK_UN);
fclose($fh_task);
  • 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-05-21T08:50:09+00:00Added an answer on May 21, 2026 at 8:50 am

    You want 'r+' (or c+ if you’re using a newer version of PHP). r+ doesn’t truncate (neither does c+), but still allows you to write.

    here’s an excerpt from when I last worked with these functions:

            /* 
              if file exists, open in read+ plus mode so we can try to lock it 
              -- opening in w+ would truncate the file *before* we could get a lock!
            */
    
            if(version_compare(PHP_VERSION, '5.2.6') >= 0) {
                $mode = 'c+';
            } else {
                //'c+' would be the ideal $mode to use, but that's only 
                //available in PHP >=5.2.6
    
                $mode = file_exists($file) ? 'r+' : 'w+';
                //there's a small chance of a race condition here
                // -- two processes could end up opening the file with 'w+'
            }
    
            //open file
            if($handle = @fopen($file, $mode)) {
                //get write lock
                flock($handle,LOCK_EX);
                //write data
                fwrite($handle, $myData);
                //truncate all data in file following the data we just wrote 
                ftruncate($handle,ftell($handle));
                //release write lock -- fclose does this automatically
                //but only in PHP <= 5.3.2
                flock($handle,LOCK_UN);
                //close file
                fclose($handle);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to develop an app that is using multithreading. Basicly, I have a
Is there a way to write a C code that allow us to determine
I am trying to implement the Devise authentication library and also add columns that
I have a general question regarding synchronized List. Lets say that in the constructor
I'm starting a Java project and need a way to completely lock down a
I need to interface with some executables that expect to be passed filenames for
I need to react on the user drag an drop action on my form.
I am writing a WPF application using C# and I need some help with
I'm parsing a large csv files - about 500 meg (many rows, many columns).
$bar = 'BAR'; apc_store('foo', $bar); var_dump(apc_fetch('foo')); Within one request this work. Now If 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.