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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:07:26+00:00 2026-05-18T04:07:26+00:00

I have the following example code <?php `echo test > /tmp/test.txt`; $f=fopen(/tmp/test.txt,w+); `rm /tmp/test.txt`;

  • 0

I have the following example code

<?php
`echo test > /tmp/test.txt`;
$f=fopen("/tmp/test.txt","w+");
`rm /tmp/test.txt`;
var_dump(fwrite($f,"test"));
fclose($f);
file_get_contents("/tmp/test.txt");

Which creates a file, opens a pointer to it, removes the file then attempts to write to it. Now I would expect the fwrite to return false or 0 since no data will be written, however it produces the following output

int(4)

Warning: file_get_contents(/tmp/test.txt): failed to open stream: No such file or directory in /Stuff/tmp/test3 on line 7

So the fwrite apparently succeeds but the file_get_contents fails as expected.

Is it expected behaviour for the fwrite to return the number of bytes to be written? If so how can I test if the write was really successful?

This is with php 5.3.3

  • 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-18T04:07:26+00:00Added an answer on May 18, 2026 at 4:07 am

    The reason it’s returning false is that the file pointer is at the end of the file after fwrite… That’s what fgets is supposed to do when it reaches the end of the file (it returns false since there is no more data to get)…

    What you need to do is add a fseek call before you call fgets:

    var_dump(fwrite($f,"test"));
    fseek($f, 0);
    var_dump(fgets($f));
    

    Edit: Now that I understand your question (and have looked at it):

    Alright, so here’s what’s happening. To understand, you must first know how Linux filesystems work. The file’s name is meaningless to the operating system. All it does is point to the INODE for the file. The inode stores the data, etc. It also stores a reference number to the number of hard-links to that file. The file is only deleted when the reference number falls to 0. I’m suspecting that opening a pointer to the file (using fopen, or other system calls) increases the reference count.

    So basically, what that means is that when you ran rm on the file, it deleted the hard-link. But since the file was still open, you could still access the inode through fwrite (hence why the write succeeded). You couldn’t access /tmp/test.txt since the hard link to that filename no longer existed. So the file became a phantom file that is only accessible by the inode. But as soon as you closed the file handle for that file (fclose, or by ending the script) the reference count fell to 0 and the inode was freed…

    So the file does exist, it’s just not accessible from the filename after you call rm…

    That’s just what I’m gathering by knowing what I know about the filesystems. I’m not saying it’s 100% accurate, but it should be a decent hypothesis…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following example code: class A(object): def __init__(self, id): self.myid = id
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
I have the following PHP code, but it's not working. I don't see any
Imagine I have the following situation: File1.php <?php include(Function.php); log(test); ?> Function.php <?php function
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
I have tried using the obvious method as outlined in the following example but
I am a newbie to Python and have come across the following example in
Say for example I have the following string: var testString = Hello, world; And
I have a list of elements (the X in the following examples) displayed either
I have following situation: I have loged user, standard authentication with DB table $authAdapter

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.