fopen 開檔
flock LOCK_EX 鎖檔
fread filesize 讀入檔案
rewind 移動檔案 point
ftruncate 0 清空檔案
fwrite 寫入檔案
flock LOCK_UN 解鎖
fclose 關檔
程式範例
$handle = fopen("file.txt","r+");
//Lock File, error if unable to lock
if(flock($handle, LOCK_EX)) {
$count = fread($handle, filesize("file.txt")); //Get Current Hit Count
$count = $count + 1; //Increment Hit Count by 1
rewind($handle); //Move file point
ftruncate($handle, 0); //Truncate the file to 0
fwrite($handle, $count); //Write the new Hit Count
flock($handle, LOCK_UN); //Unlock File
} else {
echo "Could not Lock File!";
}
//Close Stream
fclose($handle);
沒有留言:
張貼留言