PHP 8.4.6 Released!

Voting

: eight plus zero?
(Example: nine)

The Note You're Voting On

Fernando Gabrieli fgabrieli at gmail
16 years ago
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

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /