dtuzjzs3853 2011-10-10 22:26
浏览 85
已采纳

当php:// temp被多次打开时会发生什么?

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?

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.

  • 写回答

2条回答 默认 最新

  • dongmi19720408 2011-10-10 22:31
    关注

    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.

    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.

    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 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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)