note 130545 added to function.fopen
| From: | mitgath@gmail.com | Date: | Mon, 17 Nov 2025 20:54:52 +0000 |
| Subject: | note 130545 added to function.fopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-222421@lists.php.net to get a copy of this message | ||
i had to do such function to display newest log entries on top to be displayed in browser
its easy to adjust it from anonymous to normal function and/or add content on end
most important is same log can be used by many threads/processes (Swoole, ReactPHP, php_fpm)
also it had to keep reasonable size to load fast
$prependFileThreadSafe = static function (string $file, string $content, int $maxLen = 1024*500):
void {
$f = fopen($file, 'c+');
flock($f, LOCK_EX);
$prev = fread($f, $maxLen-strlen($content));
fseek($f, 0);
fwrite($f, $content . $prev);
flock($f, LOCK_UN);
fclose($f);
};
usage:
$prependFileThreadSafe('my.log', "some content\n");
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 178.219.142.108)
----
Manual Page -- https://php.net/manual/en/function.fopen.php
Edit -- https://main.php.net/note/edit/130545
Del: integrated -- https://main.php.net/note/delete/130545/integrated
Del: useless -- https://main.php.net/note/delete/130545/useless
Del: bad code -- https://main.php.net/note/delete/130545/bad+code
Del: spam -- https://main.php.net/note/delete/130545/spam
Del: non-english -- https://main.php.net/note/delete/130545/non-english
Del: in docs -- https://main.php.net/note/delete/130545/in+docs
Del: other reasons-- https://main.php.net/note/delete/130545
Reject -- https://main.php.net/note/reject/130545
Search -- https://main.php.net/manage/user-notes.php