Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems reading/writing the php://temp stream

Tags:

php

stream

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?

like image 1000
HorusKol Avatar asked May 10 '11 07:05

HorusKol


1 Answers

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

like image 189
deceze Avatar answered Oct 18 '22 15:10

deceze