note 88907 modified in function.flock by felipe

From: Date: Sat, 14 Feb 2009 22:44:47 +0000
Subject: note 88907 modified in function.flock by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150687@lists.php.net to get a copy of this message
When writing to a file, you should avoid using w+ because it would erase the contents of the file before locking If you need to write the complete file again you could use the following instead: <?php $fp = fopen('yourfile.txt', 'a') ; if (flock($fp, LOCK_EX)) { ftruncate($fp, 0) ; // <-- this will erase the contents such as 'w+' fputs($fp, 'test string') ; flock($fp, LOCK_UN) ; } fclose($fp) ; ?> Best, Fernando Gabrieli --was-- When writing to a file, you should avoid using w+ because it would erase the contents of the file before locking If you need to write the complete file again you could use the following instead: $fp = fopen('yourfile.txt', 'a') ; if (flock($fp, LOCK_EX)) { ftruncate($fp, 0) ; // <-- this will erase the contents such as 'w+' fputs($fp, 'test string') ; flock($fp, LOCK_UN) ; } fclose($fp) ; Best, Fernando Gabrieli http://php.net/manual/en/function.flock.php

« previous php.notes (#150687) next »