 <p>Since the internal pointer of the stream is at index 4 after you write the data, you need to <a href="http://php.net/fseek" rel="nofollow"><code>fseek</code></a> back to the beginning of your stream:</p> <pre><code>// fwrite fseek($memory, 0); // same as rewind($memory); // fread </code></pre>