如何检查文件句柄是否仍指向文件

Is there any way in PHP to test if a fileHandle resource still points to the file in the file system that it opened? e.g. in this code:

<?php

$filename = './foo.txt';
$fileHandle = fopen($filename, 'c');

$unlinked = unlink($filename);

if (!$unlinked) {
    echo "Failed to unlink file.";
    exit(0);
}

file_put_contents($filename, "blah blah");

// The file $filename will exist here, but I want to check that
// the $fileHandle still points to the file on the filesystem name $filename.
?>

At the end of that code, the fileHandle still exists but no longer references the file './foo.txt' on the filesystem. It instead still holds a reference to the original file that has been unlinked, i.e. it has no active entry in the filesystem, and writing data to the fileHandle will not affect the contents of the file called $filename.

Is there a (preferably atomic) operation to determine that $fileHandle is now 'invalid' in the sense that it no longer points to the original file?

It's not an atomic operation but on Linux (and other Unix systems) it's possible to check using stat on the filename, and fstat on the fileHandle, and comparing the inodes returned from each of those functions.

This doesn't work on Windows, which is less than optimal.

<?php

$filename = './foo.txt';
$fileHandle = fopen($filename, 'c');
$locked = flock($fileHandle, LOCK_EX|LOCK_NB, $wouldBlock);

if (!$locked) {
    echo "Failed to lock file.";
    exit(0);
}

$unlinked = unlink($filename);

if (!$unlinked) {
    echo "Failed to unlink file.";
    exit(0);
}

// Comment out the next line to have the 'file doesn't exist' result.
file_put_contents($filename, "blah blah ");

$originalInode = null;
$currentInode = null;

$originalStat = fstat($fileHandle);
if(array_key_exists('ino', $originalStat)) {
    $originalInode = $originalStat['ino'];
}


$currentStat = @stat($filename);
if($currentStat && array_key_exists('ino', $currentStat)) {
    $currentInode = $currentStat['ino'];
}

if ($currentInode == null) {
    echo "File doesn't currently exist.";
}
else if ($originalInode == null) {
    echo "Something went horribly wrong.";
} 
else if ($currentInode != $originalInode) {
    echo "File handle no longer points to current file.";
}
else {
    echo "inodes apparently match, which should never happen for this test case.";
}

$closed = fclose($fileHandle);

if (!$closed) {
    echo "Failed to close file.";
    exit(0);
}