PHP Reference ยท Files and I/O
PHP streams: php://stdin, php://memory and wrappers
Reference for PHP stream wrappers: php://stdin, php://stdout, php://stderr, php://memory, php://temp, php://output and php://input, with examples.
A stream is PHP's common interface for anything you can read or write in sequence: files, sockets, compressed data and more. A wrapper is the part that says where the data comes from, written as a scheme in front of the path, such as file://, http:// or php://. Functions like fopen(), file_get_contents() and fgets() accept any stream URL, so the same code can read a file, a string in memory or standard input.
The php:// family gives access to PHP's own I/O channels and to in-memory buffers. They are useful for testing, for filtering output and for command-line tools.
<?php $h = fopen('php://memory', 'w+'); fwrite($h, 'hello'); rewind($h); echo fread($h, 5);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
fopen('php://stdin', 'r'); fopen('php://stdout', 'w'); fopen('php://stderr', 'w'); fopen('php://output', 'w'); fopen('php://input', 'r'); fopen('php://memory', 'w+'); fopen('php://temp', 'w+'); fopen('php://temp/maxmemory:NN', 'w+');
| Stream | Description |
|---|---|
php://stdin |
Standard input of the process (command line). Also available as the constant STDIN. |
php://stdout |
Standard output, bypassing output buffering. Constant STDOUT. |
php://stderr |
Standard error, for diagnostics. Constant STDERR. |
php://output |
The same destination as echo, going through output buffers. |
php://input |
The raw request body in a web request (read-only). Empty on the command line. |
php://memory |
A read/write buffer kept entirely in RAM. |
php://temp |
A buffer in RAM that moves to a temporary file when it exceeds maxmemory bytes (default 2 MB). |
Return value: fopen() returns a stream resource or false.
Errors / Exceptions: opening an unknown wrapper emits a warning and returns false.
Usage notes
STDIN,STDOUTandSTDERRare predefined constants in the command-line SAPI only. In a web request they are not available, so usephp://outputorphp://inputthere.- Rewind after writing. A memory or temp stream has one position for both reading and writing. Call
rewind($h)before reading back what you wrote. - Memory streams vanish when you close the handle; nothing touches the disk.
php://tempis the safer default for data of unknown size, because it will not exhaust memory on a huge input.php://inputis how you read JSON bodies:json_decode(file_get_contents('php://input')). It does not work formultipart/form-datarequests.- Wrappers are controlled by
allow_url_fopenfor remote URLs; thephp://ones in the table above are always available, butphp://inputandphp://stdindepend on the environment. - Filters:
php://filter/read=string.toupper/resource=...applies a stream filter on the fly. Be careful when the resource part comes from user input. - Testing aid: code that accepts a file handle can be tested with a
php://memoryhandle instead of a real file.