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);
But now I want to get the descriptor for a resource opened by PHP's fopen
(). Is there a way?
The open function is the underlying primitive for the fopen and freopen functions, that create streams. Preliminary: | MT-Safe | AS-Safe | AC-Safe fd | See POSIX Safety Concepts. This function is similar to open . It returns a file descriptor which can be used to access the file named by filename .
Stdin, stdout, and stderr On a Unix-like operating system, the first three file descriptors, by default, are STDIN (standard input), STDOUT (standard output), and STDERR (standard error).
A file descriptor is an unsigned integer used by a process to identify an open file. The number of file descriptors available to a process is limited by the /OPEN_MAX control in the sys/limits. h file. The number of file descriptors is also controlled by the ulimit -n flag.
What is the File Descriptor? File descriptor is integer that uniquely identifies an open file of the process. File Descriptor table: File descriptor table is the collection of integer array indices that are file descriptors in which elements are pointers to file table entries.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With