php - 当 php ://temp is opened more than once? 时会发生什么

标签 php file

如果多次打开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://tempphp://memory 没有列出这样的异常。因此,它们将像正常的独立流一样工作。

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

关于php - 当 php ://temp is opened more than once? 时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719376/

相关文章:

python - 替换文件中的一行将另一行移动到上一级

java - 删除文件和文件夹问题

php - 为什么在 PHP 中使用 sprintf 函数?

javascript - 使用 JavaScript 安全地发布消息

php - 在结果字段上连接两个选择查询

c# - 图像调整大小 asp.net mvc 应用程序

php - 在 PHP 中发送 HTTP 响应代码的最佳方式

javascript - JSON 数字/文本解析返回一些奇怪的结果

ios - NSDictionary 根本没有数据

java - 奇怪的 ByteBuffer 输出