15

What is the meaning of php://input & php://output and when it needs to use? Please explain with an example.

hakre
  • 184,866
  • 48
  • 414
  • 792
Rahul
  • 2,259
  • 3
  • 19
  • 20
  • 1
    possible duplicate of [php://input - what does it do in fopen()?](http://stackoverflow.com/questions/7083702/php-input-what-does-it-do-in-fopen) – mario Aug 25 '11 at 07:00

2 Answers2

32

These are two of the streams that PHP provides. Streams can be used by functions like fopen, fwrite, stream_get_contents, etc.

php://input is a read-only stream that allows you to read the request body sent to it (like uploaded files or POST variables).

$request_body = stream_get_contents('php://input');

php://output is a writable stream that is sent to the server and will be returned to the browser that requested your page.

$fp = fopen('php://output', 'w');
fwrite($fp, 'Hello World!'); //User will see Hello World!
fclose($fp);

Note that if you are in the CLI, php://output will write data to the command line.

liamvictor
  • 3,161
  • 1
  • 21
  • 25
Bailey Parker
  • 15,111
  • 4
  • 51
  • 88
  • 1
    Why should one need `php://output`? Isn't it the same as using 'echo'? – robsch May 09 '18 at 13:35
  • 1
    It's the same as using "echo", _except_ it lets you use all the function you'd normally use for sending out put to a file. Sometimes, that will let you do what you want more directly or more readably. – Jeffiekins Dec 17 '18 at 16:01
2

The PHP manual has a good explanation and examples.

If you have trouble understanding something that is said there, feel free to ask again specifically - the more detailed the question, the better it is usually received.

Pekka
  • 431,103
  • 135
  • 960
  • 1,075