phpPHP
Module 26: PHP StreamsIntermediate40 min read

PHP Streams, Wrappers (php://input, php://memory), Contexts & Filters

Process gigabyte files and HTTP/JSON payloads without exhausting RAM using PHP stream wrappers, contexts, and stream filters.

Key Takeaways & Core Concepts
  • php://input: Reads raw JSON request bodies in REST APIs.
  • php://memory / php://temp: In-memory temporary streams.

PHP Streams, Wrappers (php://input, php://memory), Contexts & Filters

### 1. Why Streams Matter Streams abstract files, network sockets, HTTP, and memory behind a unified fopen/fgets/fwrite interface—allowing you to process a 5 GB CSV line-by-line in under 5 MB of RAM!

  • php://input: Reads raw JSON request bodies in REST APIs.
  • php://memory / php://temp: In-memory temporary streams.
1. Using php://memory Stream
1<?php
2$fp = fopen("php://memory", "r+");
3fwrite($fp, "Streaming in PHP 8");
4rewind($fp);
5echo stream_get_contents($fp) . "\n";
6fclose($fp);
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
6 lines
Note: Writes to and reads back from an in-memory stream without touching disk.
Practice & AssessmentPHP Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)