<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/wrappers.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'wrappers.php.php',
    1 => 'php://',
    2 => 'Accessing various I/O streams',
  ),
  'up' => 
  array (
    0 => 'wrappers.php',
    1 => 'Supported Protocols and Wrappers',
  ),
  'prev' => 
  array (
    0 => 'wrappers.ftp.php',
    1 => 'ftp://',
  ),
  'next' => 
  array (
    0 => 'wrappers.compression.php',
    1 => 'zlib://',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/wrappers/php.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="wrappers.php" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">php://</h1>
  <p class="refpurpose"><span class="refname">php://</span> &mdash; <span class="dc-title">Accessing various I/O streams</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-wrappers.php-description">
  <h3 class="title">Descrizione</h3>
  <p class="para">
   PHP provides a number of miscellaneous I/O streams that allow access to
   PHP&#039;s own input and output streams, the standard input, output and error
   file descriptors, in-memory and disk-backed temporary file streams, and
   filters that can manipulate other file resources as they are read from and
   written to.
  </p>

  <div class="refsect2 unknown-1" id="wrappers.php.std">
   <h4 class="title">php://stdin, php://stdout and php://stderr</h4>
   <p class="simpara">
    <var class="filename">php://stdin</var>, <var class="filename">php://stdout</var> and
    <var class="filename">php://stderr</var> allow direct access to the corresponding
    input or output stream of the PHP process.  The stream references a
    duplicate file descriptor, so if you open <var class="filename">php://stdin</var>
    and later close it, you close only your copy of the descriptor-the actual
    stream referenced by <strong><code><a href="reserved.constants.php#constant.stdin">STDIN</a></code></strong> is unaffected.
    It is
    recommended that you simply use the constants <strong><code><a href="reserved.constants.php#constant.stdin">STDIN</a></code></strong>,
    <strong><code><a href="reserved.constants.php#constant.stdout">STDOUT</a></code></strong> and <strong><code><a href="reserved.constants.php#constant.stderr">STDERR</a></code></strong> instead of
    manually opening streams using these wrappers.
   </p>
   <p class="simpara">
    <var class="filename">php://stdin</var> is read-only, whereas
    <var class="filename">php://stdout</var> and <var class="filename">php://stderr</var> are
    write-only.
   </p>
  </div>


  <div class="refsect2 unknown-4" id="wrappers.php.input">
   <h4 class="title">php://input</h4>
   <p class="simpara">
    <var class="filename">php://input</var> is a read-only stream that allows you to
    read raw data from the request body.
    <var class="filename">php://input</var> is not available in POST requests with
    <code class="literal">enctype=&quot;multipart/form-data&quot;</code> if
    <a href="ini.core.php#ini.enable-post-data-reading" class="link">enable_post_data_reading</a>
    option is enabled.
   </p>
  </div>


  <div class="refsect2 unknown-7" id="wrappers.php.output">
   <h4 class="title">php://output</h4>
   <p class="para">
    <var class="filename">php://output</var> is a write-only stream that allows you to
    write to the output buffer mechanism in the same way as
    <span class="function"><a href="function.print.php" class="function">print</a></span> and <span class="function"><a href="function.echo.php" class="function">echo</a></span>.
   </p>
  </div>


  <div class="refsect2 unknown-10" id="wrappers.php.fd">
   <h4 class="title">php://fd</h4>
   <p class="simpara">
    <var class="filename">php://fd</var> allows direct access to the given file
    descriptor. For example, <var class="filename">php://fd/3</var> refers to file
    descriptor 3.
   </p>
  </div>


  <div class="refsect2 unknown-13" id="wrappers.php.memory">
   <h4 class="title">php://memory and php://temp</h4>
   <p class="simpara">
    <var class="filename">php://memory</var> and <var class="filename">php://temp</var> are
    read-write streams that allow temporary data to be stored in a file-like
    wrapper. One difference between the two is that
    <var class="filename">php://memory</var> will always store its data in memory,
    whereas <var class="filename">php://temp</var> will use a temporary file once the
    amount of data stored hits a predefined limit (the default is 2 MB). The
    location of this temporary file is determined in the same way as the
    <span class="function"><a href="function.sys-get-temp-dir.php" class="function">sys_get_temp_dir()</a></span> function.
   </p>
   <p class="simpara">
    The memory limit of <var class="filename">php://temp</var> can be controlled by
    appending <code class="literal">/maxmemory:NN</code>, where <code class="literal">NN</code> is
    the maximum amount of data to keep in memory before using a temporary
    file, in bytes.
   </p>
   <div class="caution"><strong class="caution">Attenzione</strong>
    <p class="simpara">
     Some PHP extensions may require a standard IO stream,
     and may attempt to cast a given stream to a standard IO stream.
     This cast can fail for memory streams as it requires the C
     <span class="function"><strong>fopencookie()</strong></span> function to be available.
     This C function is <em>not</em> available on Windows.
    </p>
   </div>
  </div>


  <div class="refsect2 unknown-16" id="wrappers.php.filter">
   <h4 class="title">php://filter</h4>
   <p class="simpara">
    <var class="filename">php://filter</var> is a kind of meta-wrapper designed to
    permit the application of <a href="filters.php" class="link">filters</a> to a
    stream at the time of opening.  This is useful with all-in-one file
    functions such as <span class="function"><a href="function.readfile.php" class="function">readfile()</a></span>,
    <span class="function"><a href="function.file.php" class="function">file()</a></span>, and <span class="function"><a href="function.file-get-contents.php" class="function">file_get_contents()</a></span>
    where there is otherwise no opportunity to apply a filter to the stream
    prior the contents being read.
   </p>
   <p class="para">
    The <var class="filename">php://filter</var> target takes the following parameters
    as part of its path. Multiple filter chains can be specified on one path.
    Please refer to the examples for specifics on using these parameters.
   </p>
   <p class="para">
    <table class="doctable table">
     <caption><strong>php://filter parameters</strong></caption>
     
      <thead>
       <tr>
        <th>Name</th>
        <th>Description</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>
         <code class="literal">resource=&lt;stream to be filtered&gt;</code>
        </td>
        <td>
         This parameter is required. It specifies the stream that you would
         like to filter.
        </td>
       </tr>

       <tr>
        <td>
         <code class="literal">read=&lt;filter list to apply to read chain&gt;</code>
        </td>
        <td>
         This parameter is optional. One or more filter names can be provided
         here, separated by the pipe character (<code class="literal">|</code>).
        </td>
       </tr>

       <tr>
        <td>
         <code class="literal">write=&lt;filter list to apply to write chain&gt;</code>
        </td>
        <td>
         This parameter is optional. One or more filter names can be provided
         here, separated by the pipe character (<code class="literal">|</code>).
        </td>
       </tr>

       <tr>
        <td>
         <code class="literal">&lt;filter list to apply to both chains&gt;</code>
        </td>
        <td>
         Any filter lists which are not prefixed by <code class="literal">read=</code>
         or <code class="literal">write=</code> will be applied to both the read and
         write chains as appropriate.
        </td>
       </tr>

      </tbody>
     
    </table>

   </p>
  </div>

 </div>


 <div class="refsect1 options" id="refsect1-wrappers.php-options">
  <h3 class="title">Opzioni</h3>
  <p class="para">
   <table class="doctable table">
    <caption><strong>
     Wrapper Summary (for <code class="literal">php://filter</code>, refer to the
     summary of the wrapper being filtered)
    </strong></caption>
    
     <thead>
      <tr>
       <th>Attribute</th>
       <th>Supported</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>Restricted by <a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">allow_url_fopen</a></td>
       <td>No</td>
      </tr>

      <tr>
       <td>Restricted by <a href="filesystem.configuration.php#ini.allow-url-include" class="link">allow_url_include</a></td>
       <td>
        <code class="literal">php://input</code>,
        <code class="literal">php://stdin</code>,
        <code class="literal">php://memory</code> and
        <code class="literal">php://temp</code> only.
       </td>
      </tr>

      <tr>
       <td>Allows Reading</td>
       <td>
        <code class="literal">php://stdin</code>,
        <code class="literal">php://input</code>,
        <code class="literal">php://fd</code>,
        <code class="literal">php://memory</code> and
        <code class="literal">php://temp</code> only.
       </td>
      </tr>

      <tr>
       <td>Allows Writing</td>
       <td>
        <code class="literal">php://stdout</code>,
        <code class="literal">php://stderr</code>,
        <code class="literal">php://output</code>,
        <code class="literal">php://fd</code>,
        <code class="literal">php://memory</code> and
        <code class="literal">php://temp</code> only.
       </td>
      </tr>

      <tr>
       <td>Allows Appending</td>
       <td>
        <code class="literal">php://stdout</code>,
        <code class="literal">php://stderr</code>,
        <code class="literal">php://output</code>,
        <code class="literal">php://fd</code>,
        <code class="literal">php://memory</code> and
        <code class="literal">php://temp</code> only. (Equivalent to writing)
       </td>
      </tr>

      <tr>
       <td>Allows Simultaneous Reading and Writing</td>
       <td>
        <code class="literal">php://fd</code>,
        <code class="literal">php://memory</code> and
        <code class="literal">php://temp</code> only.
       </td>
      </tr>

      <tr>
       <td>Supports <span class="function"><a href="function.stat.php" class="function">stat()</a></span></td>
       <td>
        No.  However, <code class="literal">php://memory</code> and
        <code class="literal">php://temp</code> support <span class="function"><a href="function.fstat.php" class="function">fstat()</a></span>.
       </td>
      </tr>

      <tr>
       <td>Supports <span class="function"><a href="function.unlink.php" class="function">unlink()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>Supports <span class="function"><a href="function.rename.php" class="function">rename()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>Supports <span class="function"><a href="function.mkdir.php" class="function">mkdir()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>Supports <span class="function"><a href="function.rmdir.php" class="function">rmdir()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>Supports <span class="function"><a href="function.stream-select.php" class="function">stream_select()</a></span></td>
       <td>
        <code class="literal">php://stdin</code>,
        <code class="literal">php://stdout</code>,
        <code class="literal">php://stderr</code>,
        <code class="literal">php://fd</code> and
        <code class="literal">php://temp</code> only.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>
 

 <div class="refsect1 examples" id="refsect1-wrappers.php-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 php://temp/maxmemory</strong></p>
   <div class="example-contents"><p>
    This optional parameter allows setting the memory limit before
    <var class="filename">php://temp</var> starts using a temporary file.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Set the limit to 5 MB.<br /></span><span style="color: #0000BB">$fiveMBs </span><span style="color: #007700">= </span><span style="color: #0000BB">5 </span><span style="color: #007700">* </span><span style="color: #0000BB">1024 </span><span style="color: #007700">* </span><span style="color: #0000BB">1024</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://temp/maxmemory:</span><span style="color: #0000BB">$fiveMBs</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #DD0000">'r+'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fputs</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"hello\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Read what we have written.<br /></span><span style="color: #0000BB">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-2">
   <p><strong>Example #2 php://filter/resource=&lt;stream to be filtered&gt;</strong></p>
   <div class="example-contents"><p>
    This parameter must be located at
    the end of your <var class="filename">php://filter</var> specification and
    should point to the stream which you want filtered.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* This is equivalent to simply:<br />  readfile("http://www.example.com");<br />  since no filters are actually specified */<br /><br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/resource=http://www.example.com"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-3">
   <p><strong>Example #3 php://filter/read=&lt;filter list to apply to read chain&gt;</strong></p>
   <div class="example-contents"><p>
    This parameter takes one or more
    filternames separated by the pipe character <code class="literal">|</code>.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* This will output the contents of<br />  www.example.com entirely in uppercase */<br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/read=string.toupper/resource=http://www.example.com"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* This will do the same as above<br />  but will also ROT13 encode it */<br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/read=string.toupper|string.rot13/resource=http://www.example.com"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-4">
   <p><strong>Example #4 php://filter/write=&lt;filter list to apply to write chain&gt;</strong></p>
   <div class="example-contents"><p>
    This parameter takes one or more
    filternames separated by the pipe character <code class="literal">|</code>.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* This will filter the string "Hello World"<br />  through the rot13 filter, then write to<br />  example.txt in the current directory */<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/write=string.rot13/resource=example.txt"</span><span style="color: #007700">,</span><span style="color: #DD0000">"Hello World"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-5">
   <p><strong>Example #5 php://memory and php://temp are not reusable</strong></p>
   <div class="example-contents"><p>
    <var class="filename">php://memory</var> and <var class="filename">php://temp</var>
    are not reusable, i.e. after the streams have been closed there is no way
    to refer to them again.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://memory'</span><span style="color: #007700">, </span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://memory'</span><span style="color: #007700">); </span><span style="color: #FF8000">// prints nothing</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-6">
   <p><strong>Example #6 php://input to read JSON data from the request body</strong></p>
   <div class="example-contents"><p>
    This example demonstrates how to read raw JSON data from POST, PUT and
    PATCH requests using <var class="filename">php://input</var>.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$input </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://input"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$json_array </span><span style="color: #007700">= </span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(<br />  </span><span style="color: #0000BB">json</span><span style="color: #007700">: </span><span style="color: #0000BB">$input</span><span style="color: #007700">,<br />  </span><span style="color: #0000BB">associative</span><span style="color: #007700">: </span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />  </span><span style="color: #0000BB">flags</span><span style="color: #007700">: </span><span style="color: #0000BB">JSON_THROW_ON_ERROR<br /></span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Received JSON data: "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$json_array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>

</div><?php manual_footer($setup); ?>