php://内存& php://temp;在后续句柄创建时保留流数据

发布于 2024-11-27 08:09:30 字数 608 浏览 2 评论 0原文

这个问题与我的新发现密切相关,关于 问题

有没有办法在句柄之间保留 php://memoryphp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林散布 2024-12-04 08:09:31

处理程序是唯一的,因此您必须传递处理程序,或者(上帝禁止)将处理程序保留为全局

$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+');

小鸟爱天空丶 2024-12-04 08:09:30

打开伪流 php://tempphp://memory 之一总是会打开一个新流,这意味着您以这种方式打开的每个流都是唯一的。因此,您无法读取之前写入另一个流的内容。

Opening one of the pseudo-streams php://temp or php://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.

过潦 2024-12-04 08:09:30

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文