Jump to content

Possible PHP bug with fopen() and fwrite()?


rlelek

Recommended Posts

 

Greetings all,

 

I'm completely stumped...so I'd like to run this issue by you guys (and gals!).

I have encountered an odd issue, and I have no idea why this is doing what it's doing.

 

When I execute the following code, all by itself, in an "index.php" file (with no other files around...completely isolated!)

I get a result of "testtest"  :confused:

 

$log_file = 'all.txt';
$handle = fopen($log_file, 'a');
fwrite($handle, 'test');
fclose($handle);

 

Note: I delete the file each time I execute the script (in a browser). I am also the only one running this script.

 

I've tried running on PHP 5.2.x and PHP 5.3.x

I have also tried on my machine as well as a separate, remote machine.

 

Is the script running twice?

If so, what's causing it?

 

Any help is appreciated as always!

 

Ryan

Link to comment
Share on other sites

If you don't need to append to the file you should use another mode than "a", try use "w", this will place the pointer in the start of the file, and delete the other content.

 

Try something like this

 


$file_loc = "myfile.txt";
if (file_exists($file_loc))
{
    $handle = fopen($file_loc, "a")
    fwrite($handle
}
else
{
     $handle = fopen($file_loc, "w")
     fwrite($handle, "the first content\n");
}

if ($handle)
     fclose($handle);

 

I haven't tested it, just a suggestion ;)

Link to comment
Share on other sites

Well, I did some more poking around, and found the solution.

 

Apparently, one of those many FireFox add-ons was making an additional request to the same page (for debugging purposes I'm sure).

:facewall: :facewall: :facewall:

 

Must have been one of those early-morning moments!

 

For those of you that have issues with anything, try disabling FireFox Add-ons or try using a different browser!  ::)

 

Ryan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.