note 88907 added to function.flock
| From: | FernandoGabrielifgabrieliatgmail at osu1 dot php dot net | Date: | Thu, 12 Feb 2009 19:06:39 +0000 |
| Subject: | note 88907 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150589@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:
$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
----
Server IP: 201.216.232.3
Probable Submitter: 190.55.147.209
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- https://master.php.net/note/edit/88907
Del: integrated -- https://master.php.net/note/delete/88907/integrated
Del: useless -- https://master.php.net/note/delete/88907/useless
Del: bad code -- https://master.php.net/note/delete/88907/bad+code
Del: spam -- https://master.php.net/note/delete/88907/spam
Del: non-english -- https://master.php.net/note/delete/88907/non-english
Del: in docs -- https://master.php.net/note/delete/88907/in+docs
Del: other reasons-- https://master.php.net/note/delete/88907
Reject -- https://master.php.net/note/reject/88907
Search -- https://master.php.net/manage/user-notes.php