多次打开php://temp会发生什么? [英] What happens when php://temp is opened more than once?

查看:199
本文介绍了多次打开php://temp会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果php://temp(或php://memory)文件多次打开,则句柄是否指向同一文件?还是每个句柄都是唯一的?

If the php://temp (or php://memory) file is opened more than once, will the handles point to the same file? Or will each handle be unique?

我在php文档中找不到答案,所以我要去

I couldn't find an answer in the php docs, so I'm going to write up a test script to find out. I figured it's worth asking here so someone else can find the answer easily.

推荐答案

每个句柄都指向一个独立的流.示例:

Each handle points to an independent stream. Example:

$a = fopen('php://memory', 'w+');
$b = fopen('php://memory', 'w+');

fwrite($a, 'foo');
fwrite($b, 'bar');

rewind($a);
rewind($b);

$a_text = stream_get_contents($a);  //=> "foo"
$b_text = stream_get_contents($b);  //=> "bar"

fclose($a);
fclose($b);

这在任何地方都没有明确说明,但在流和包装的文档中却是隐含的.

This is not explicitly documented anywhere, but it is implicit in the documentation for streams and wrappers.

通常,从官方 php有关流的文档中,很明显,对于流的标准情况,每个文件句柄都与它自己的独立流相关联.

From the official php documentation on streams in general, it is clear that for the standard case of streams, each file handle is associated with it's own independent stream.

关于IO流包装程序的文档中,它列出了可能的包装程序注意异常的发生.前三个(stdin,stdout,stderr)列出了一个例外:

And in the documentation on IO stream wrappers, it lists the possible wrappers noting exceptions as they occur. There is an exception listed for the first three (stdin, stdout, stderr):

php://stdin,php://stdout和php://stderr允许直接访问 PHP进程的相应输入或输出流.流 引用重复的文件描述符,所以如果您打开php://stdin, 稍后关闭它,您只关闭描述符的副本-实际的 STDIN引用的流不受影响.

php://stdin, php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected.

但没有列出php://tempphp://memory的此类异常.因此,它们可以像正常的独立流一样工作.

But no such exception is listed for php://temp or php://memory. Hence it follows that these would work like normal independent streams.

此外,这些页面上有一些评论进一步暗示了这些信息流的独立性.

Also, there are some comments on these pages that further imply the Independence of these streams.

这篇关于多次打开php://temp会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆