stream_get_meta_data
stream_get_meta_data(resource $stream): array
Stream metadata: returns the stream's "dossier" — timed_out/blocked/eof/unread_bytes/stream_type/wrapper_type/mode/seekable/uri (plus crypto on TLS streams); "a stream is a resource, and a resource has a dossier" — the first tool for diagnosing timeouts and wrapper behaviour.
C C23
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void) {
FILE *fp = tmpfile();
fputs("abc", fp);
fflush(fp); // flush stdio buffers so fd-level size is accurate
/* fstat on the descriptor is C's "stream metadata":
* size and file type live at the fd layer. */
struct stat st;
fstat(fileno(fp), &st);
printf("size: %lld\n", (long long)st.st_size);
printf("regular: %s\n", S_ISREG(st.st_mode) ? "true" : "false");
return 0;
}
size: 3 regular: true
C++ C++26
#include <cstdio>
#include <iostream>
#include <sys/stat.h>
#include <unistd.h>
int main() {
FILE *fp = tmpfile();
fputs("abc", fp);
std::fflush(fp); // fd-level metadata only sees flushed bytes
// fstat on the descriptor: size and type at the fd layer;
// <filesystem> cannot help here because tmpfile has no path.
struct stat st;
fstat(fileno(fp), &st);
std::cout << "size: " << static_cast<long long>(st.st_size) << '\n';
std::cout << "regular: " << (S_ISREG(st.st_mode) ? "true" : "false") << '\n';
return 0;
}
size: 3 regular: true
C# v11.0.100
// FileStream exposes stream-level metadata as object properties;
// the temp path is random and intentionally never printed.
string path = Path.GetTempFileName();
try
{
File.WriteAllText(path, "abc");
using FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
Console.WriteLine($"size: {fs.Length}");
Console.WriteLine($"can-seek: {fs.CanSeek}");
Console.WriteLine($"readable: {fs.CanRead}");
}
finally
{
File.Delete(path);
}
size: 3 can-seek: True readable: True
Go v1.27.4
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.CreateTemp("", "codeabc-meta-")
if err != nil {
panic(err)
}
defer os.Remove(f.Name()) // random path: only metadata is printed
f.WriteString("abc")
f.Sync()
info, _ := f.Stat() // os.File.Stat: the fd-level dossier
fmt.Println("size:", info.Size())
fmt.Println("regular:", info.Mode().IsRegular())
}
size: 3 regular: true
Java v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
public class Main {
public static void main(String[] args) throws Exception {
// NIO2 file attributes: the file-level dossier (the random
// temp path itself is intentionally never printed).
Path p = Files.createTempFile("codeabc-meta-", ".bin");
try {
Files.write(p, "abc".getBytes());
BasicFileAttributes meta = Files.readAttributes(p, BasicFileAttributes.class);
System.out.println("size: " + meta.size());
System.out.println("regular: " + meta.isRegularFile());
} finally {
Files.deleteIfExists(p);
}
}
}
size: 3 regular: true
JavaScript v26.13.1
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
// fs.statSync is the file-level dossier; the pid-derived temp path
// is never printed, so the output stays environment-independent.
const p = path.join(os.tmpdir(), `codeabc-meta-${process.pid}.bin`);
try {
fs.writeFileSync(p, 'abc');
const meta = fs.statSync(p);
console.log(`size: ${meta.size}`);
console.log(`regular: ${meta.isFile()}`);
} finally {
fs.unlinkSync(p);
}
size: 3 regular: true
PHP v8.6.0
$fp = fopen('php://memory', 'r+');
fwrite($fp, 'abc');
$meta = stream_get_meta_data($fp);
echo $meta['wrapper_type'], "\n"; // "PHP": which wrapper layer backs this stream
echo $meta['stream_type'], "\n"; // "MEMORY": the underlying implementation
echo $meta['uri'], "\n"; // the URI the stream was opened from
echo var_export($meta['seekable'], true), "\n";
echo var_export($meta['eof'], true), "\n";
echo var_export($meta['timed_out'], true), "\n";
echo $meta['mode'], "\n";
echo var_export($meta['blocked'], true), "\n";
PHP MEMORY php://memory true false false w+b true
Python v3.15.2
import os
import stat
import tempfile
# os.fstat(fd) is the fd-level dossier; the random temp path is
# captured only for cleanup and never printed.
fd, path = tempfile.mkstemp(prefix="codeabc-meta-")
try:
with os.fdopen(fd, "w+b") as f:
f.write(b"abc")
f.flush()
st = os.fstat(f.fileno())
print("size:", st.st_size)
print("regular:", stat.S_ISREG(st.st_mode))
finally:
os.unlink(path)
size: 3 regular: True
Rust v1.99.1
use std::fs::{self, File};
use std::io::Write;
fn main() {
// File::metadata is the fd-level dossier; the pid-derived temp
// path is only cleaned up and never printed.
let path = std::env::temp_dir().join(format!("codeabc-meta-{}.bin", std::process::id()));
let mut f = File::create(&path).unwrap();
f.write_all(b"abc").unwrap();
f.sync_all().unwrap();
let meta = f.metadata().unwrap();
println!("size: {}", meta.len());
println!("regular: {}", meta.is_file());
fs::remove_file(&path).ok();
}
size: 3 regular: true
TypeScript v26.13.1
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
// fs.statSync is the file-level dossier; the pid-derived temp path
// is never printed, so the output stays environment-independent.
const p: string = path.join(os.tmpdir(), `codeabc-meta-${process.pid}.bin`);
try {
fs.writeFileSync(p, 'abc');
const meta: fs.Stats = fs.statSync(p);
console.log(`size: ${meta.size}`);
console.log(`regular: ${meta.isFile()}`);
} finally {
fs.unlinkSync(p);
}
size: 3 regular: true
Language differences
PHP: returns the stream's "dossier" — timed_out/blocked/eof/unread_bytes/stream_type/wrapper_type/wrapper_data/mode/seekable/uri, plus a crypto array on TLS streams (manual Return Values); the example uses php://memory, whose fields are fixed values, keeping the assertions environment-independent.
eof semantics: on sockets eof can be true while unread_bytes is still > 0 — test readability with feof(), not this field (manual Note); unread_bytes is PHP's own internal buffer size and the manual says it "shouldn't be used in a script".
wrapper_data: wrapper-specific data — the received response headers under the http wrapper (this is how file_get_contents reads response headers), the certificate chain under the ssl wrapper.
C: fstat(fileno(fp)) is the fd-level dossier (size/type); the stdio buffer is a separate layer — without fflush you may read a stale size (the example flushes first).
C++: same as C; <filesystem> only resolves metadata by path, which a pathless tmpfile cannot provide.
C#: FileStream.Length/CanSeek/CanRead are stream-object properties while File.GetAttributes/FileInfo are file-level — the two layers mirror PHP's stream fields vs fstat.
Go: os.File.Stat() returns FileInfo (Size/Mode/IsDir...); connection metadata (peer address etc.) lives on the concrete net.Conn type.
Java: Files.readAttributes (file-level NIO2 dossier) vs the stream's available()/channel; HTTP metadata sits on the Response object (headers/statusCode).
JS/TS: Node's fs.statSync (file level) vs a ReadStream's bytesRead/path (stream level); HTTP response metadata is on response.headers/statusCode.
Python: os.fstat(fd) (fd level) vs the file object's name/mode/closed (object level); the stat module decodes the st_mode bits.
Rust: File::metadata() (file level) is layered apart from wrappers like BufReader; the read/write cursor position comes separately from Seek::stream_position().