我有一个锁定某个文件夹中所有文件的函数:
function lockFolder_files($folder='',$task=''){
global $file_array;//I need to use this var outside the function too
$file_array=glob($folder . '*_che.php');//this lists all files ending with "_che.php" in the array from folder1.
//now do a foreach on the array and open the file, and lock it:
foreach($file_array as $path){
$lock=fopen($path,"a+")//open with append mode
if($task=="lock"){
flock($lock,LOCK_EX);
}
elseif($task=="unlock"){
flock($lock,LOCK_UN);
}
}//end of foreach
if(count($file_array)==0){echo"no files were found in the folder"; return false;}
}//end of function
所以我称这个函数为:
lockFolder_files("blah1/blah/myfolder","lock");
//do what i need to do with the array of files locked ($file_array)
lockFolder_files("blah1/blah/myfolder","unlock");//unlock all the files
现在它似乎找到了文件夹中的所有文件,将它们分配给数组,但由于某种原因,它似乎没有锁定文件。在测试之后(使用sleep()并尝试使用其他脚本写入文件)flock()
似乎对文件没有任何影响......
为什么会发生这种情况?
由于
答案 0 :(得分:1)
两件事:
flock()
访问权限同步时,才允许使用* nix进行文件锁定。