So given this simple example:
<?php
$memory = fopen('php://memory', 'r+');
fwrite($memory, 'asdf', 4);
$value = fread($memory, 3);
var_dump($value);
I was expecting $value to contain the string "asd" but instead I get an empty string. Is there anything obvious about this example that needs to change? Is my expectation incorrect? If $value should indeed contain "asd" what might cause this to happen (a configuration/php.ini issue perhaps)?
Since the internal pointer of the stream is at index 4 after you write the data, you need to
fseekback to the beginning of your stream: