note 74155 modified in function.flock by danbrown

From: Date: Mon, 18 May 2009 00:36:50 +0000
Subject: note 74155 modified in function.flock by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155394@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. <?php 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; } ?> --was-- 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; } http://php.net/manual/en/function.flock.php

« previous php.notes (#155394) next »