douchensi8625 2015-05-11 15:26
浏览 220
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dongsou4301 2015-05-11 15:49
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?