<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Reads remainder of a stream into a string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-filter-remove.html">stream_filter_remove</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-get-filters.html">stream_get_filters</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.stream-get-contents" class="refentry"> <div class="refnamediv"> <h1 class="refname">stream_get_contents</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">stream_get_contents</span> — <span class="dc-title">Reads remainder of a stream into a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.stream-get-contents-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>stream_get_contents</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$maxlength</code><span class="initializer"> = -1</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = -1</span></span> ]] )</div> <p class="para rdfs-comment"> Identical to <span class="function"><a href="function.file-get-contents.html" class="function">file_get_contents()</a></span>, except that <span class="function"><strong>stream_get_contents()</strong></span> operates on an already open stream resource and returns the remaining contents in a string, up to <em><code class="parameter">maxlength</code></em> bytes and starting at the specified <em><code class="parameter">offset</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.stream-get-contents-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">handle</code></em> (<span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>)</span> <dd> <p class="para"> A stream resource (e.g. returned from <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>) </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">maxlength</code></em> (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)</span> <dd> <p class="para"> The maximum bytes to read. Defaults to -1 (read all the remaining buffer). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">offset</code></em> (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)</span> <dd> <p class="para"> Seek to the specified offset before reading. If this number is negative, no seeking will occur and reading will start from the current position. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.stream-get-contents-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a string or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.stream-get-contents-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.1.0</td> <td> The <em><code class="parameter">offset</code></em> was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.stream-get-contents-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4020"> <p><strong>Example #1 <span class="function"><strong>stream_get_contents()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$stream </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// print all the page starting at the offset 10<br /> </span><span style="color: #007700">echo </span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br />}<br /><br /><br />if (</span><span style="color: #0000BB">$stream </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.net'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// print the first 5 bytes<br /> </span><span style="color: #007700">echo </span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.stream-get-contents-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is binary-safe.</span></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.stream-get-contents-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li> <li class="member"> <span class="function"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - Binary-safe file read</span></li> <li class="member"> <span class="function"><a href="function.fpassthru.html" class="function" rel="rdfs-seeAlso">fpassthru()</a> - Output all remaining data on a file pointer</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-filter-remove.html">stream_filter_remove</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-get-filters.html">stream_get_filters</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>