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);
?>

Updated on: 22-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements