note 41263 modified in function.flock by danbrown

From: Date: Mon, 18 May 2009 00:47:04 +0000
Subject: note 41263 modified in function.flock by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155429@lists.php.net to get a copy of this message
Following is based on below comments. When something goes wrong, script will perform backup of writen data to randomly named file in temp dir. <?php function safewrite($filename, $data){ $rand = microtime(); $rand = md5($rand); $temp = fopen("temp/$rand", "w"); fwrite($temp, $data); fclose($temp); $otw = fopen($filename, "a+"); if(flock($otw, LOCK_EX)){ ftruncate($otw, 0); if(!fwrite($otw, $data)) $err = 1; flock($otw, LOCK_UN); fclose($otw); } else { $err = 1; } if($err == 1 || (filesize($filename) == 0 && strlen($data) <> 0)){ die("<b>There was an error while writing to $filename. Contact site administrator!</b>"); } else { unlink("temp/$rand"); } } ?> Hope it helps. --was-- Following is based on below comments. When something goes wrong, script will perform backup of writen data to randomly named file in temp dir. <? function safewrite($filename, $data){ $rand = microtime(); $rand = md5($rand); $temp = fopen("temp/$rand", "w"); fwrite($temp, $data); fclose($temp); $otw = fopen($filename, "a+"); if(flock($otw, LOCK_EX)){ ftruncate($otw, 0); if(!fwrite($otw, $data)) $err = 1; flock($otw, LOCK_UN); fclose($otw); } else { $err = 1; } if($err == 1 || (filesize($filename) == 0 && strlen($data) <> 0)){ die("<b>There was an error while writing to $filename. Contact site administrator!</b>"); } else { unlink("temp/$rand"); } } ?> Hope it helps. http://php.net/manual/en/function.flock.php

« previous php.notes (#155429) next »