
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP php://
Introduction
The php://wrapper enableaccess to various I/O streams. This includes standard input, output and error streams. In-memory, disk backed and filtered streams are lso accessed with php:// protocol.
Standard streams
php://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.
php://input
php://input allows read-only acess to raw data contained in HTTP request body. Note that same data is available in $HTTP_POST_RAW-DATA variable (which is now deprecated). However, php://input is not available for enctype attribute is set to multipart/form-data
php://output
This wrapper represents write-only tream, allowing buffer mechanism, similar to print and echo statements.
php://fd
a file descriptor is accessible through this wrapper. The standard streams STDIN, STDOUT and STDERR are assigned file descriptors 1,2 and 3. Every other stream is assigned incrementing file descriptor. Hence php://fd/5 refers to file descriptor 5
php://memory
This is a read/write stream that allows data to be temporarily stored in memory. The php://temp wrapper is similar. However, in case of the latter, data is stored in a temporary file instead of memory.
php://filter
This wrapper permits application of filter to a stream when it is opened. Filters are especially useful with readfile(), file_get_contents() and file() functions.
Examples
In following example, console input is read from php://stdin and output displayed with php://stdout
<?php $file=fopen("php://stdin","r"); $x=fread($file,10); echo $x; $out=fopen("php://stdout","w"); fwrite($out, $x); fclose($file); ?>
php://input stream wrapper allows fetching raw data from HTTP request. In following example, HTML form posts data to a PHP script with POST method
<html> <body> <form action="testscript.php" method="POST"> <input type="text" name="name"> <input type="text" name="age"> <input type ="submit" value="submit"> </form> </body> </html>
The PHP script to retrieve raw HTTP data is as follows −
<?php $json = file_get_contents("php://input"); $data = json_decode($json); print_r($json); ?>
- Related Articles
- Upload file with php to another php server
- PHP Generators.
- PHP Overloading
- PHP Traits
- PHP $_GET
- PHP $GLOBALS
- PHP $_SERVER
- PHP superglobals
- PHP References
- PHP Constants
- PHP Expressions
- PHP Tags
- PHP Iterables
- PHP NULL
- PHP Objects.
