13
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");

I can understand this that /home/rasmus/file.txt and /home/rasmus/file.gif are the file path.

But what these mean:

php://input
php://temp

in

$objInputStream = fopen("php://input", "r");
$objTempStream = fopen("php://temp", "w+b");

What do they do?

laukok
  • 47,545
  • 146
  • 388
  • 689

3 Answers3

9

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATAis not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

Check out the manual: http://php.net/manual/en/wrappers.php.php

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
adritha84
  • 927
  • 1
  • 7
  • 15
  • 1
    The last sentence is no longer accurate. Setting [`enable_post_data_reading = 0`](http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading) will disable the parsing of the `php://input` stream into `$_FILES`, making `php://input` readable for multipart requests. – Dave Nov 08 '17 at 15:50
6

php://temp stores data in a temporary file which is only accessible for the duration of the script's execution. It is a real file, but is cleaned up as soon as the script terminates unlike a true file opened with fopen(), that will persist on the filesystem.

php://input is used for reading the raw HTTP request body, without having the $_POST and $_SERVER variables abstracted out. The php://input stream would give access to the entire HTTP request as the server handed it to the PHP interpreter.

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
  • 3
    Not entirely true: *"php://temp will use a temporary file once the amount of data stored hits a predefined limit (the default is 2 MB)."* – netcoder Aug 16 '11 at 19:20
3

Those are stream wrappers and allow you to read from various streams. The reading and writting to stream is performed in the same way as with the file (some limitation can exists, for example not every stream wrapper supports fseek). php://input gives you access to the raw HTTP data (it's available in $HTTP_RAW_POST_DATA if server is configured to prepopulate it). Best - read the corresponding section in the documentation

Maxim Krizhanovsky
  • 24,757
  • 5
  • 49
  • 85