Page 1 of 1

file writing

Posted: Sat May 13, 2006 10:20 pm
by s.dot

Code: Select all

//	UPDATE THE HTML FILE

//	open the file
$handle = fopen("../../cron_results/birthdays.html", "w");

//	lock it
flock($handle, LOCK_EX);

//	write the contents
fwrite($handle, $str);

//	release the lock
flock($handle, LOCK_UN);

//	close the file
fclose($handle);

echo 'Birthday list successfully updated.';
This will be ran on a 10 minute cron on a pretty heavily accessed page. It's output goes to an html file that will be included on the index page. During the file writing process will the users experience any errors? I realize that this will only take like a couple hundredths of a second to do (if that), but will the users experience any problems?

No Prob?

Posted: Sat May 13, 2006 10:39 pm
by tr0gd0rr
AFAIK, locking the file from within php will not prevent Linux's ability to read the file. So the only time there might be a race condition is when the file is actually being written. It may depend on your filesystem, but I would imagine all the Linux filesystems would have no trouble at all.

I do know by experience that using IIS 4.0 on Windows NT with ASP can cause files to be corrupted and 404 errors to pop up if a user is accessing a page while it is saving to the disk. :lol:

I suppose your situation would be similar to if you updated the file via FTP.

Posted: Sat May 13, 2006 10:44 pm
by s.dot
Well this is a small portion of the page. At the bottom. and i'm using require(), so I guess the worst that would happen is that the page would terminate at that point. In which case I should use include() instead.

Has anyone had experience with this/read docs about this situation?

Posted: Sun May 14, 2006 3:53 pm
by s.dot
I put the file writing process (the code above) inside of an infinite loop.

Code: Select all

<?php

while(TRUE){
	require 'cron_30second.php';
}

?>
So the file writing process was occuring almost simultaneously. I then refreshed the page that this file was included on a bunch of times and didn't experience any problems.

Does that mean it's OK?

Is there a better way to test it, other than refreshing the page a bunch of times while the loop is running?

Posted: Sun May 14, 2006 6:05 pm
by Ollie Saunders

Code: Select all

$handle = fopen("../../cron_results/birthdays.html", "w");
Is is possible this can return false from a lock already in progress? or would that happen at fwrite?

if that is the case you may want to add something like this:

Code: Select all

$f = "../../cron_results/birthdays.html";
$attempts = 20;
for(
   $i=20;
   $handle = fopen($f, "w") == false || $i <$attempts; // is that condition right?
   $i++) sleep(2);
oh and btw looks like in the manual you should check the success of the flock call and the file is automatically unlocked when you fclose.

need help with fput

Posted: Sun May 14, 2006 9:02 pm
by shebi
Can anybody help me with this?
I'm making a forum by writing and reading into/ from a text file.. everything works well untill somebody hits enter. "feof" takes enter as a new line and everything gets messed up. How can I deal with "enter" and "feof"?

Posted: Mon May 15, 2006 12:09 am
by s.dot
Ah, well see, the file writing will write a very small string (less than 1kb) to an html file once every 30 seconds. This will be the only script ever writing to this file, and it will only be ran once per 30 seconds, and executed by the server.

So it should never be locked by another script at the same time.

Posted: Mon May 15, 2006 2:10 am
by Ollie Saunders
So it should never be locked by another script at the same time.
Why exactly are you locking at all?

Posted: Mon May 15, 2006 4:56 am
by s.dot
because the file that im writing to is included on a pretty regularly trafficked page.
i don't want the page to read the file at the same time i'm writing it.

Posted: Mon May 15, 2006 5:15 am
by Ollie Saunders
oh ok.
this any use?