1
を動作していないファイルパスの配列に群れを()を使用して:私は特定のフォルダ内のすべてのファイルをロックする機能を持っている
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()
はファイルに何の影響も及ぼしていないようです...
これは何が起こっているのですか?
おかげ
です。では、関数が終了したら、ファイルを開いたままにしておくことはできますか?ありがとう。 –
たとえば、ファイルハンドルを含む配列を返すことができます。この場合、ハンドルは依然としてコードによって到達可能であるため、閉じられません。 – ckruse