note 120880 added to function.file-put-contents

From: Date: Mon, 27 Mar 2017 13:57:26 +0000
Subject: note 120880 added to function.file-put-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-208951@lists.php.net to get a copy of this message
This php.net example could be confusing: <?php file_put_contents($file, $person, FILE_APPEND | LOCK_EX); ?> You do not need LOCK_EX if you write only to a file (like log files). Multiple writing processes are already queued. Test it on your own by calling this script 4 times simultaneously: <?php function string_rand($len, $split="\n") { return substr(chunk_split(bin2hex(openssl_random_pseudo_bytes(ceil($len / 2))), 1023, $split), 0, $len); } ini_set('memory_limit', '200M'); if (!file_put_contents('../cache4/file.txt', string_rand(102400000), FILE_APPEND)) { exit('file_put_contents() error!'); } clearstatcache(); echo 'filesize: ' . filesize('../cache4/file.txt') . PHP_EOL; $fp = fopen('../cache4/file.txt', 'r'); if ($fp) { while (($line = fgets($fp)) !== false) { if (strlen($line) != 1024) { exit('Line length error!'); } } fclose($fp); } ?> You will not see an error, the last called script needs the longest time (as it is the last one in the queue) and the final size will be 409600000 bytes as it should be. ---- Server IP: 72.52.91.14 Probable Submitter: 2003:e0:a3e7:a800:788f:2e63:4722:b6ef ---- Manual Page -- http://php.net/manual/en/function.file-put-contents.php Edit -- https://master.php.net/note/edit/120880 Del: integrated -- https://master.php.net/note/delete/120880/integrated Del: useless -- https://master.php.net/note/delete/120880/useless Del: bad code -- https://master.php.net/note/delete/120880/bad+code Del: spam -- https://master.php.net/note/delete/120880/spam Del: non-english -- https://master.php.net/note/delete/120880/non-english Del: in docs -- https://master.php.net/note/delete/120880/in+docs Del: other reasons-- https://master.php.net/note/delete/120880 Reject -- https://master.php.net/note/reject/120880 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208951) next »