php - fopen 并获取系统文件描述符

标签 php file fopen file-descriptor

我想在使用 open 打开文件时获取返回资源的系统文件描述符。我假设描述符是一个 INT 值,通常在/dev/fd/

我知道我可以通过执行以下操作来读取描述符:

fread("php://fd/$descriptor", $buflen);

但现在我想获取由 PHP 的 fopen() 打开的资源的描述符。有办法吗?

最佳答案

这是一种相当 hacky 的解决方法,但它确实有效!

function fd($realpath) {
  $dir = '/proc/self/fd/';
  if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
          $filename = $dir . $file;
          if (filetype($filename) == 'link' && realpath($filename) == $realpath) {
            closedir($dh);
            return $file;
          }
      }
      closedir($dh);
  }
  return FALSE;
}

关于php - fopen 并获取系统文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600811/

相关文章:

php - Paypal : Get a response of "txn_type= recurring payment " instead of "txn_type= recurring_payment_profile_created" for the first Time

linux - 如何使用 bash 命令将文件名列表写入 .lst 文件

java - Android读入与app打包的.txt文件

c - 如何找到二进制文件的结尾?

python - 在 Python 上使用 CSV 文件打开函数

php - 使用 PHP 和 MySQL 按字母顺序检索阿拉伯语单词

PHP 使用 PDO 将图像上传到 mysql 数据库?

c - 文件操作中输出未正确显示

java - 如何从 jar 文件中读取文件?

c - Fopen 二进制文件失败