stream_get_contents
stream_get_contents(resource $stream, ?int $length = null, int $offset = -1): string|false
The handle-level "read to end": reads from the stream's current position to EOF (the counterpart of file_get_contents, which reads a whole path at once); length caps the bytes and offset seeks first.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
FILE *f = tmpfile(); // anonymous temp file, auto-deleted on fclose
fputs("abcdefghij", f);
fseek(f, 3, SEEK_SET);
// C has no read-to-EOF helper: fread in a loop is the stream_get_contents equivalent
char buf[64], all[128];
size_t n, len = 0;
while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {
memcpy(all + len, buf, n);
len += n;
}
printf("%.*s\n", (int)len, all); // from the current position to the end
rewind(f);
n = fread(buf, 1, 4, f);
printf("%.*s\n", (int)n, buf); // length-limited: first 4 bytes
fclose(f);
return 0;
}
defghij abcd
C++ C++26
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
int main() {
std::istringstream in("abcdefghij"); // an in-memory stand-in for a file stream
in.seekg(3);
// istreambuf-iterators copy from the current position to the end
std::string rest((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
std::cout << rest << "\n";
in.seekg(0);
char buf[5] = {0};
in.read(buf, 4); // length-limited read
std::cout << buf << "\n";
return 0;
}
defghij abcd
C# v11.0.100
using System.Text;
using var ms = new MemoryStream(Encoding.UTF8.GetBytes("abcdefghij"));
ms.Position = 3; // the stream cursor, like PHP's file position
using (var r = new StreamReader(ms, Encoding.UTF8, leaveOpen: true)) {
Console.WriteLine(r.ReadToEnd()); // from the current position to the end
}
ms.Position = 0;
var buf = new byte[4];
ms.Read(buf, 0, 4); // length-limited read
Console.WriteLine(Encoding.UTF8.GetString(buf));
defghij abcd
Go v1.27.4
package main
import (
"fmt"
"io"
"strings"
)
func main() {
r := strings.NewReader("abcdefghij")
r.Seek(3, io.SeekStart)
rest, _ := io.ReadAll(r) // reads from the current position to the end
fmt.Println(string(rest))
r.Seek(0, io.SeekStart)
buf := make([]byte, 4)
r.Read(buf) // length-limited read
fmt.Println(string(buf))
}
defghij abcd
Java v27.0.1
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws java.io.IOException {
byte[] data = "abcdefghij".getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream in = new ByteArrayInputStream(data);
in.skip(3);
System.out.println(new String(in.readAllBytes(), StandardCharsets.UTF_8)); // current position -> end
ByteArrayInputStream in2 = new ByteArrayInputStream(data);
byte[] buf = in2.readNBytes(4); // length-limited read
System.out.println(new String(buf, StandardCharsets.UTF_8));
}
}
defghij abcd
JavaScript v26.13.1
const fs = require("node:fs");
// fs.readSync with position = null reads from the CURRENT offset — stream_get_contents semantics
fs.writeFileSync("stream-demo.tmp", "abcdefghij");
// reopen read-only: the cursor starts at 0, like a freshly fopen()ed handle
const fd = fs.openSync("stream-demo.tmp", "r");
fs.readSync(fd, Buffer.alloc(3), 0, 3, null); // consumes "abc": the cursor advances to 3
const rest = Buffer.alloc(7);
fs.readSync(fd, rest, 0, 7, null); // null position: from the current position to the end
console.log(rest.toString());
const head = Buffer.alloc(4);
fs.readSync(fd, head, 0, 4, 0); // explicit position: length-limited read
console.log(head.toString());
fs.closeSync(fd);
fs.rmSync("stream-demo.tmp");
defghij abcd
PHP v8.6.0
$tmp = tmpfile();
fwrite($tmp, 'abcdefghij');
fseek($tmp, 3);
echo stream_get_contents($tmp), "\n"; // reads the REMAINDER from the current position
rewind($tmp);
echo stream_get_contents($tmp, 4), "\n"; // length-limited: first 4 bytes
fclose($tmp);
defghij abcd
Python v3.15.2
import tempfile
with tempfile.TemporaryFile(mode="w+") as f:
f.write("abcdefghij")
f.seek(3)
print(f.read()) # no argument: reads the REMAINDER from the current position
f.seek(0)
print(f.read(4)) # length-limited read
defghij abcd
Rust v1.99.1
use std::io::{Read, Seek, SeekFrom};
fn main() {
let mut c = std::io::Cursor::new("abcdefghij");
c.seek(SeekFrom::Start(3)).unwrap();
let mut rest = String::new();
c.read_to_string(&mut rest).unwrap(); // from the current position to the end
println!("{}", rest);
c.seek(SeekFrom::Start(0)).unwrap();
let mut buf = [0u8; 4];
c.read(&mut buf).unwrap(); // length-limited read
println!("{}", String::from_utf8_lossy(&buf));
}
defghij abcd
TypeScript v26.13.1
import * as fs from "node:fs";
// fs.readSync with position = null reads from the CURRENT offset — stream_get_contents semantics
fs.writeFileSync("stream-demo.tmp", "abcdefghij");
// reopen read-only: the cursor starts at 0, like a freshly fopen()ed handle
const fd: number = fs.openSync("stream-demo.tmp", "r");
fs.readSync(fd, Buffer.alloc(3), 0, 3, null); // consumes "abc": the cursor advances to 3
const rest: Buffer = Buffer.alloc(7);
fs.readSync(fd, rest, 0, 7, null); // null position: from the current position to the end
console.log(rest.toString());
const head: Buffer = Buffer.alloc(4);
fs.readSync(fd, head, 0, 4, 0); // explicit position: length-limited read
console.log(head.toString());
fs.closeSync(fd);
fs.rmSync("stream-demo.tmp");
defghij abcd
Language differences
PHP: reads from the CURRENT POSITION to EOF on an open handle (the counterpart of file_get_contents, which reads a whole path at once); length caps the byte count and offset seeks first (-1 = no seek); binary-safe (manual Note).
Division of labour: fread($fp, n) reads a fixed n bytes, fgets reads one line, stream_get_contents reads the remainder.
C: no read-to-EOF helper — a fread loop until EOF is the equivalent; tmpfile() anonymous temp files are auto-deleted on fclose.
C++: building a string from (istreambuf_iterator<char>(in), {}) is the idiom for current-position-to-end; the example uses istringstream to stay off disk — ifstream files behave identically.
Go: io.ReadAll reads to EOF; Rust: Read::read_to_string; Java: InputStream.readAllBytes (Java 9+); C#: StreamReader.ReadToEnd — four names, one semantics.
JS/TS: Node fs.readSync(fd, buf, off, len, position) with position = null reads from the current offset — the example writes the file, reopens it read-only (cursor at 0) and reads twice; note that a positioned read is a pread and does NOT advance the cursor.
Python: a no-argument f.read() IS the read-to-end, and f.read(n) is the length-limited form — no separate function exists.
Caveat: "read to end" on a network stream may block forever — pair it with stream_set_timeout/stream_select or check unread_bytes first (manual user note).