note 47889 added to control-structures.while
| From: | corychristison | Date: | Fri, 03 Dec 2004 23:08:37 +0000 |
| Subject: | note 47889 added to control-structures.while | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81344@lists.php.net to get a copy of this message | ||
While can do wonders if you need something to queue writing to a file while something else has
access to it.
Here is my simple example:
<?php
function write ($data, $file, $write_mode="w") {
$lock = $file . ".lock";
// run the write fix, to stop any clashes that may occur
write_fix($lock);
// create a new lock file after write_fix() for this writing session
touch( $lock );
// write to your file
$open = fopen($file, $write_mode);
fwrite($open, $data);
fclose($open);
// kill your current lock
unlink($lock);
}
function write_fix ($lock_file) {
while( file_exists($lock_file){
// do something in here?
// maybe sleep for a few microseconds
// to maintain stability, if this is going to
// take a while ?? [just a suggestion]
}
}
?>
This method is not recommended for use with programs that will be needing a good few seconds to
write to a file, as the while function will eat up alot of process cycles. However, this method
does work, and is easy to implement. It also groups the writing functions into one easy to use
function, making life easier. :-)
----
Manual Page -- http://www.php.net/manual/en/control-structures.while.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47889
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47889&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47889&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47889&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47889&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47889&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47889&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47889&report=yes
Search -- http://master.php.net/manage/user-notes.php