我無法準確理解ed文件是如何鎖定的。我鎖定一個簡單的文本文件,像這樣:爲什麼PHP仍然可以讀/寫這個鎖定的文件?
// file_lock.php
$filename = "data.txt";
$delay = 5;
$lock = fopen($filename, "w");
if ($lock === FALSE || !flock($lock, LOCK_EX | LOCK_NB, $block) || $block) {
echo "ERROR: Can not open or lock " . $filename . "\n";
sleep($delay);
}
else {
echo "SUCCESS: " . $filename . " locked.\n";
sleep($delay);
flock($lock, LOCK_UN);
echo $filename . " unlocked\n";
}
然後,從另一個腳本,我檢查文件is_writable()
/is_readable()
:
// test.php
$filename = "data.txt";
if (is_readable($filename)) echo "File is readable\n";
else echo "File is not readable\n";
if (is_writable($filename)) echo "File is writable\n";
else echo "File is not writable\n";
但是當我運行他們,他們都認爲他們有訪問:
$ php file_lock.php & php test.php
SUCCESS: data.txt locked.
File is readable
File is writable
data.txt unlocked
的文檔here和here是對細節非常缺乏,我缺少什麼?我不正確地使用羊羣嗎?執行功能is_readable()
和is_writable()
檢查與確定讀取/寫入無關的內容?
只是好奇,你有沒有嘗試寫入test.php腳本中的文件? –
可讀/可寫是IIRC純粹的權限檢查,它們不檢查文件的鎖定狀態? –
@Tom我沒有嘗試與文件實際交互,我認爲它不會工作,因爲文件_should_被鎖定。 – Will