打开并获取系统文件描述符 [英] fopen and getting system file descriptor
本文介绍了打开并获取系统文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用open打开文件时,我想获取返回资源的系统文件描述符.我假设描述符是一个INT值,通常在/dev/fd/
I want to get a system file descriptor of the returned resource when I open a file using open. I assume the descriptor is an INT value which is normally inside /dev/fd/
我知道我可以通过以下操作从描述符中读取内容:
I know that I can read from the descriptor by doing something like:
fread("php://fd/$descriptor", $buflen);
但是现在我想获取由PHP的fopen
()打开的资源的描述符.有办法吗?
But now I want to get the descriptor for a resource opened by PHP's fopen
(). Is there a way?
推荐答案
这是一个很棘手的解决方法,但是有效!
This is a rather hacky way around it but it works!
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;
}
这篇关于打开并获取系统文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文