读取/写入 php://temp 流时遇到问题

2022-08-30 16:06:32

我在 PHP 5.3.2 中读取和写入流时遇到问题php://temp

我基本上有:

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

我得到的唯一输出是string(0) ""

难道我不应该拿回我的“测试”吗?


答案 1

php://temp不是文件路径,而是一个伪协议,在使用时始终会创建新的随机临时文件。实际上被完全忽略了。包装器接受的唯一额外“参数”是 。您需要将文件句柄保留在打开的临时流周围,否则它将被丢弃:/testphp://temp/maxmemory:n

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

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


答案 2

每次使用 fopen 获取处理程序时,php://temp 的内容都会被刷新。使用 rewind() 和 stream_get_contents() 来获取内容。或者,使用普通的缓存器,如APC或memcache:)


推荐