Welcome Guest, Not a member yet? Register   Sign In
fwrite() being screwy
#1

[eluser]nZac[/eluser]
Heres an iteresting one for you, here is the code:


Code:
$msg = 'TEST';
$fp = fopen('./logs/somefile.php','ab');

flock($fp, LOCK_EX);    
fwrite($fp, $msg);
flock($fp, LOCK_UN);
fclose($fp);

within the file the output is this:

TESTTEST

I did a little research and found this is a known bug. Anyone know how to get past it?
#2

[eluser]cahva[/eluser]
I did your test on Windows, Linux(Debian) and FreeBSD and I got just TEST in the file. What version of PHP are you running and on what os?
#3

[eluser]Kepler[/eluser]
I would try:

Code:
$msg = 'TEST';
$fp = fopen('./logs/somefile.php','ab');

flock($fp, LOCK_EX);    
fwrite($fp, $msg);
fflush($fp); // in case the next statement fork()s, the cache will be written
flock($fp, LOCK_UN);
fclose($fp);
#4

[eluser]nZac[/eluser]
@Cahva
Version: PHP Version 5.2.6 running as a CGI
OS: Linux

@Kepler
That didn't work. Thanks though... any other suggestions.

I really don't wanna use file_get/put_contents, that is way to slow!
#5

[eluser]Kepler[/eluser]
The only other thing I can think of is that the code section is being called twice. Try adding a counter to see if the 2 "TEST"s are actually separate calls:

Code:
if (($counter = $this->session->userdata('counter')) === false)
{
    $counter = 0;
}
$msg = 'TEST'.$counter;
$fp = fopen('./logs/somefile.php','ab');

flock($fp, LOCK_EX);    
fwrite($fp, $msg);
flock($fp, LOCK_UN);
fclose($fp);
$this->session->set_userdata('counter', ($counter + 1));

If your file contains:

TEST0TEST1

then you are calling that section of code twice. If the file contains:

TEST0TEST0

then you are back to where you started.
#6

[eluser]nZac[/eluser]
TEST0TEST0

This is really frustrating because I have no idea why this would happen!

I ran some other tests and found out this.

When I run


Code:
$msg = 'TEST';
$fp = ('./logs/somefile.php');

file_put_contents($fp,$msg);

I get just TEST

And this:

Code:
$msg = 'TEST';
$fp = ('./logs/somefile.php');

file_put_contents($fp,$msg, FILE_APPEND);


I get TESTTEST on one submit ( used the counter suggested by Kelper to double check).

Any more suggestions? FYI this is doing this OUTSIDE of the CI framework, so its not a CI thing it must be a php thing.

Should I ask my host to re-install PHP?
#7

[eluser]nZac[/eluser]
I ran another test and here is what I found:

BACKGROUND INFO:
Here is what my file structure looks like.

/
/dev
/dev/logs/
/public_html/
/public_html/logs/


Both my development version of the site and the live version are on the same server and PHP install. However On the live site the code works as should but on the dev site it doesn't?

What could possible cause this?
#8

[eluser]Kepler[/eluser]
The only time I have seen this behavior is when a process fork()s (unix) after the file is opened and a write is made but before the file is closed. The fork copies the cached write contents which is why you see double entries. Since the fflush() didn't solve the problem, maybe a fork() happened before the write().

Try commenting out both flock statements to see if they are the culprits. If you only get one "TEST" when you comment out flock(), try using an external lock file as described by "Kuzma dot Deretuke at gmail dot com" in the comments section of the PHP flock documentation page.
#9

[eluser]nZac[/eluser]
Still get the same output.
#10

[eluser]Kepler[/eluser]
try adding a debug_print_backtrace() statement to see how/when this code is called. It may give you some insight to what is going on.




Theme © iAndrew 2016 - Forum software by © MyBB