I got sick of rewriting the flock code for every fopen call, so I made a function that does it for me. Does anyone see any problems with it? The only modes my script uses are 'r' 'w' and 'a', no +'s.
function open($file, $mode) {
global $config;
clearstatcache();
if($config['flock']) {
$fp = @fopen($file, $mode) or die("<b>FATAL:</b> Failed to open $file in mode $mode. Please check your paths.");
if($mode == 'r') {
flock($fp,LOCK_SH);
}
else if($mode == 'a'){
flock($fp,LOCK_EX);
clearstatcache();
fseek($fp,filesize($file));
}
else if($mode == 'w') {
flock($fp,LOCK_EX);
fseek($fp,0);
}
}
else {
$fp = @fopen($file, $mode) or die("<b>FATAL:</b> Failed to open $file in mode $mode. Please check your paths.");
}
return($fp);
}
Thanks for your opinions 😃