Having problems reading/writing the php://temp stream
Asked Answered
E

4

30

I'm having trouble with reading and writing the php://temp stream in PHP 5.3.2

I basically have:

file_put_contents('php://temp/test', 'test');
var_dump(file_get_contents('php://temp/test'));

The only output I get is string(0) ""

Shouldn't I get my 'test' back?

Erich answered 10/5, 2011 at 7:20 Comment(0)
M
32

php://temp is not a file path, it's a pseudo protocol that always creates a new random temp file when used. The /test is actually being ignored entirely. The only extra "arguments" the php://temp wrapper accepts is /maxmemory:n. You need to keep a file handle around to the opened temp stream, or it will be discarded:

$tmp = fopen('php://temp', 'r+');
fwrite($tmp, 'test');
rewind($tmp);
fpassthru($tmp);
fclose($tmp);

See http://php.net/manual/en/wrappers.php.php#refsect1-wrappers.php-examples

Mound answered 10/5, 2011 at 7:36 Comment(4)
Found what I needed - using vfsStreamErich
Why then the manual stated that file_put_contents() can accept the url wrappers just like fopen()Bryophyte
@Accountantم …‽ 🤷‍♂️ It can perfectly fine. Just this particular wrapper behaves in a very specific way. There are others which work just fine with it. The manual doesn’t say that every wrapper makes sense to use with file_put_contents.Mound
@Mound 'Just this particular wrapper behaves in a very specific way' Yes, I finally found a note in the manual that used exactly the same code example in the question and explains that php://temp and php://memory are not reusable. I guess they mean once file_put_contents() closes the stream internally, I can't get it again by file_get_contents().Bryophyte
L
12

Each time, when you use fopen to get handler, content of php://temp will be flushed. Use rewind() and stream_get_contents() to get content. Or, use normal cachers, like APC or memcache :)

Loess answered 10/5, 2011 at 7:41 Comment(1)
As a side note, you can rewind implicitly with stream_get_contents($stream, -1, 0). That will read the entire stream from the beginning.Golgotha
N
4

Finally found a documented small note, that explains why

Example 5 at the PHP Manual used almost your exact same code sample and says

php://memory and php://temp are not reusable, i.e. after the streams have been closed there is no way to refer to them again.

file_put_contents('php://memory', 'PHP');
echo file_get_contents('php://memory'); // prints nothing

I guess this means that file_put_contents() closes the stream internally, which makes file_get_contents() unable to recover the data in the stream again

Nervine answered 4/2, 2019 at 22:5 Comment(0)
A
3

I know this is late, but in addition to @OZ_'s answer, i just discovered that 'fread' works too, after you rewind.

$handle = fopen('php://temp', 'w+');

fwrite($handle, 'I am freaking awesome');

fread($handle); // returns '';

rewind($handle); // resets the position of pointer

fread($handle, fstat($handle)['size']); // I am freaking awesome
Assert answered 24/10, 2018 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.