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

  • Home
  • SEARCH
  • 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 8848575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:27:15+00:00 2026-06-14T12:27:15+00:00

If you do use a simple fopen/fwrite() such as: $ref = @fopen(‘/path/to/file’, ‘x+b’); //

  • 0

If you do use a simple fopen/fwrite() such as:

$ref = @fopen('/path/to/file', 'x+b'); // returns false if already exists
if ($ref) {
    fwrite($ref, 'A');
    sleep(5);
    fwrite($ref, 'B');
}

How can you check that the file pointer is still valid if some other process deletes the file during the sleep?

I have considered using a simple is_file() check, but that wouldn’t say if another process did the unlink() then created a new file with the same path.

For reference, fwrite seems to still return true, even though the file has been unlink()’ed.

Edit: Just to clarify, the sleep(5) is just a random time, it could be just a couple of ms, in the case of a race condition.

  • 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-14T12:27:17+00:00Added an answer on June 14, 2026 at 12:27 pm

    I’ve just made a test, and it seems that fstat is the solution:

    $fd = fopen("/path/to/file", "w");
    var_dump(fstat($fd)); // shows ['nlink'] => int(1)
    sleep(60);
    // in a shell console, remove the file
    var_dump(fstat($fd)); // shows ['nlink'] => int(0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to use simple TCP/IP sockets connections over/with Net.TCP WCF binding? Idea is –
I'm trying to use Simple Savant within my application, to use SimpleDB I currently
We often use simple enumerations to represent a state on our entities. The problem
Is there a way to use simple sql queries on ASP MVC without using
I've always thought it was great that I could use simple iconic unicode characters
I've used ITK with c++ some time ago and I'm trying to use simple
use LWP::Simple; use Parallel::ForkManager; @links=( [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe,SweetHome3D-2.1-windows.exe], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg,SweetHome3D-2.1-macosx.dmg], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip,SweetHome3DViewer-2.1.zip], ); # Max 30 processes for
I use a simple pipe. I read with a while, 1 char at a
A trivial use of PHP and frwite() to create/write to a text file. However,
I am trying to read in a simple flat file, it has 738,627 records

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.