note 74155 added to function.flock

From: Date: Wed, 28 Mar 2007 00:52:07 +0000
Subject: note 74155 added to function.flock
Groups: php.notes 
Request: Send a blank email to php-notes+get-124860@lists.php.net to get a copy of this message
I've been testing a few custom file access functions but I always liked the simplicity of file_get_contents(). Of course it doesn't seem to respect any file locks created with flock(). I created the function below to wrap around file_get_contents() so it can support locked files. It's an odd way of doing it but it works for me. function flock_get_contents($filename){ $return = FALSE; if(is_string($filename) && !empty($filename)){ if(is_readable($filename)){ if($handle = @fopen($filename, 'r')){ while(!$return){ if(flock($handle, LOCK_SH)){ if($return = file_get_contents($filename)){ flock($handle, LOCK_UN); } } } fclose($handle); } } } return $return; } ---- Server IP: 216.235.15.211 Probable Submitter: 24.68.139.166 ---- Manual Page -- http://www.php.net/manual/en/function.flock.php Edit -- https://master.php.net/note/edit/74155 Del: integrated -- https://master.php.net/note/delete/74155/integrated Del: useless -- https://master.php.net/note/delete/74155/useless Del: bad code -- https://master.php.net/note/delete/74155/bad+code Del: spam -- https://master.php.net/note/delete/74155/spam Del: non-english -- https://master.php.net/note/delete/74155/non-english Del: in docs -- https://master.php.net/note/delete/74155/in+docs Del: other reasons-- https://master.php.net/note/delete/74155 Reject -- https://master.php.net/note/reject/74155 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124860) next »