特定のフォルダ内のすべてのファイルをロックする機能があります。
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()
、ファイルに何の影響も及ぼさないようです。
なぜこれが起こっているのか考えはありますか?
ありがとう