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

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序