Skip to content
Tutorials Center
My learning

Topics

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
<?php
$h = fopen('php://memory', 'w+');
fwrite($h, 'hello');
rewind($h);
echo fread($h, 5);

Try it

PHP In-memory files with php://memory and php://temp

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.

PHP stdout, stderr and the output stream

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

PHP
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, STDOUT and STDERR are predefined constants in the command-line SAPI only. In a web request they are not available, so use php://output or php://input there.
  • 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://temp is the safer default for data of unknown size, because it will not exhaust memory on a huge input.
  • php://input is how you read JSON bodies: json_decode(file_get_contents('php://input')). It does not work for multipart/form-data requests.
  • Wrappers are controlled by allow_url_fopen for remote URLs; the php:// ones in the table above are always available, but php://input and php://stdin depend 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://memory handle instead of a real file.