php://内存& php://temp;在后续句柄创建时保留流数据
这个问题与我的新发现密切相关,关于 问题。
有没有办法在句柄之间保留 php://memory
或 php://temp
的流内数据?我读到(在我无法立即获取的地方)上述流的后续打开会清除现有数据。
$mem1 = fopen('php://memory', 'r+');
fwrite($mem1, 'hello world');
rewind($mem1);
fpassthru($mem1); // "hello world"
$mem2 = fopen('php://memory', 'r+');
rewind($mem2);
fpassthru($mem2); // empty
所以我的问题又是,在创建新的句柄时,是否有办法强制现有数据保留在流中?
(鉴于这是可能的,后者对 fpassthru()
的调用当然会转储 hello world
)
This question is closely related to my new findings, regarding this question.
Is there any way to preserve the in stream data of php://memory
or php://temp
between handles? I read (somewhere I can't source off hand) that subsequent openings of the aforementioned streams clears existing data.
$mem1 = fopen('php://memory', 'r+');
fwrite($mem1, 'hello world');
rewind($mem1);
fpassthru($mem1); // "hello world"
$mem2 = fopen('php://memory', 'r+');
rewind($mem2);
fpassthru($mem2); // empty
So again my question is, is there anyway to force existing data to persist in stream when creating a new handle to it?
(The latter call to fpassthru()
would of course dump hello world
given this is possible)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理程序是唯一的,因此您必须传递处理程序,或者(上帝禁止)将处理程序保留为全局
$GLOBALS['my_global_memory_stream']=fopen('php://memory','r+');
The handlers are unique, so you'll have to pass the handler, or (god forbid) keep the handler global
$GLOBALS['my_global_memory_stream']=fopen('php://memory','r+');
打开伪流
php://temp
或php://memory
之一总是会打开一个新流,这意味着您以这种方式打开的每个流都是唯一的。因此,您无法读取之前写入另一个流的内容。Opening one of the pseudo-streams
php://temp
orphp://memory
always opens a new stream, what means, that every stream your open this way is unique. So you can't read the content of the stream you have previously written to another one.如果您需要内存中的虚拟流来保存数据,您可以使用 https://github.com/mikey179/vfsStream - 虽然它主要用于测试 I/O 操作,但它应该满足您的要求 - 它将数据存储在由虚拟 URL 标识的内部对象中,因此您可以通过访问相同的 URL 来访问内存中的相同数据。
If you need in-memory virtual stream that persists data you can use https://github.com/mikey179/vfsStream - although it's mainly used for testing I/O operations it should fulfill your requirements - it stores data within internal objects which are identified by virtual URLs so you can access same data in memory by accessing same URL.