📄 08c09-2.php
字号:
<?php// A function to creating a secondary locking file for usfunction lock($filename) { // Remove the ability for user aborts until we are finished ignore_user_abort(true); // Attempt to open, and/or create, the lock file: if (!($fp = fopen("{$filename}.lock", 'w'))) { die ('ERROR: Could not open file!'); } // Now attempt to get an exclusive lock if (!(flock($fp, LOCK_EX))) { die('ERROR: Could not get exclusive lock!'); } // We now have the lock, return the locked file pointer return $fp;}// Now create a function that will handle unlocking for us:function unlock($filepointer) { // Unlock the secondary lock file: if (!(flock($filepointer, LOCK_UN))) { die('ERROR: Could undo exclusive lock!'); } // Remove the restriction on user aborts: ignore_user_abort(false);}// Now use these functions to do a full read/write loop on a file// incrementing a number for each time the file is written:// Obtain the lock:$lock = lock('data.counter');// Read in the file:$filedata = @file('data.counter');// Determine our new count:$count = isset($filedata[0]) ? $filedata[0] + 1 : 1;// Now we can write our data:file_put_contents('data.counter', $count);// Release the exclusive lockunlock($lock);// Now echo out our file contents so that we can see what happened:echo '<pre>';readfile('data.counter');echo '</pre>';?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -