如果多次打开php://temp
(或php://memory
)文件,句柄会指向同一个文件吗?或者每个 handle 都是独一无二的?
我在 php 文档中找不到答案,所以我要去 write up a test script to find out .我认为在这里提问是值得的,这样其他人就可以轻松找到答案。
最佳答案
每个句柄指向一个独立的流。示例:
$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);
这在任何地方都没有明确记录,但它隐含在流和包装器的文档中。
来自官方php documentation on streams一般来说,很明显,对于流的标准情况,每个文件句柄都与其自己的独立流相关联。
并且在 documentation on IO stream wrappers ,它列出了可能的包装器,在它们发生时记录了异常。前三个(stdin、stdout、stderr)列出了一个异常(exception):
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://temp
或 php://memory
没有列出这样的异常。因此,它们将像正常的独立流一样工作。
此外,这些页面上的一些评论进一步暗示了这些流的独立性。
关于php - 当 php ://temp is opened more than once? 时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719376/