PHP/resource/stream: Difference between revisions

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Created page with "{{fmt/title|stream resources in PHP}} ==About== Although there are a lot of functions that handle streams in PHP, how to properly use these functions is often not explained ve..."
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{fmt/title|stream resources in PHP}}
{{fmt/title|stream-type "resource" demiobjects in PHP}}
==About==
==About==
Although there are a lot of functions that handle streams in PHP, how to properly use these functions is often not explained very well. I'm having to reverse-engineer a lot through trial and error.
Although there are a lot of functions that handle streams in PHP, how to properly use these functions is often not explained very well. I'm having to reverse-engineer a lot through trial and error.
==Tentative Conclusions==
==Tentative Conclusions==
* If a stream is set to blocking mode (with <code>[https://www.php.net/manual/en/function.stream-set-blocking.php stream_set_blocking]({{arg|stream resource}}, TRUE);</code>), then EoS (end-of-stream) is indicated by the return of an empty string.
* If a stream is set to blocking mode (with <code>{{l/pfx|PHP/fx/|stream_set_blocking}}({{arg|stream resource}}, TRUE);</code>), then EoS (end-of-stream) is indicated by the return of an empty string.
** Questions about how it works in non-blocking mode:
** Questions about how it works in non-blocking mode:
*** How do we detect EoS in ''non''-blocking mode?
*** How do we detect EoS in ''non''-blocking mode? Some tentative leads:
**** We could set the stream with an event-callback (see {{l/pfx|PHP/fx/|stream_notification_callback}}) and look for [https://www.php.net/manual/en/stream.constants.php#constant.stream-notify-completed STREAM_NOTIFY_COMPLETED] events. '''PROBLEM''': docs say this is only supported for http and ftp.
**** Maybe <code>{{l/pfx|PHP/fx/|feof}}()</code> would work? '''PROBLEM''': although docs say this can be used on a stream, they also say it has to have been opened with <code>{{l/pfx|PHP/fx/|fopen}}()</code> or <code>{{l/pfx|PHP/fx/|fsockopen}}()</code>, which rules out process-fed streams created with functions like <code>{{l/pfx|PHP/fx/|ssh2_exec}}()</code>.
*** Does empty-string still correctly signal that the process has completed?
*** Does empty-string still correctly signal that the process has completed?
**** I ''think'' we have to assume that this is true for <code>{{l/pfx|PHP/fx/|stream_get_contents}}()</code>: it will never return empty as long as the process is active. (Maybe in non-blocking mode, it blocks until there is at least one byte, or something?)
*** What happens if the process has ''not'' completed (so there's no new data yet) -- how can we tell that there might be more?
*** What happens if the process has ''not'' completed (so there's no new data yet) -- how can we tell that there might be more?
==Links==
===Reference===
* [https://www.php.net/manual/en/ref.stream.php Stream Functions]

Latest revision as of 01:36, 11 February 2026

stream-type "resource" demiobjects in PHP

{{#set: page title=stream-type "resource" demiobjects in PHP }}

About

Although there are a lot of functions that handle streams in PHP, how to properly use these functions is often not explained very well. I'm having to reverse-engineer a lot through trial and error.

Tentative Conclusions

  • If a stream is set to blocking mode (with stream_set_blocking(<stream resource>, TRUE);), then EoS (end-of-stream) is indicated by the return of an empty string.
    • Questions about how it works in non-blocking mode:
      • How do we detect EoS in non-blocking mode? Some tentative leads:
        • We could set the stream with an event-callback (see stream_notification_callback) and look for STREAM_NOTIFY_COMPLETED events. PROBLEM: docs say this is only supported for http and ftp.
        • Maybe feof() would work? PROBLEM: although docs say this can be used on a stream, they also say it has to have been opened with fopen() or fsockopen(), which rules out process-fed streams created with functions like ssh2_exec().
      • Does empty-string still correctly signal that the process has completed?
        • I think we have to assume that this is true for stream_get_contents(): it will never return empty as long as the process is active. (Maybe in non-blocking mode, it blocks until there is at least one byte, or something?)
      • What happens if the process has not completed (so there's no new data yet) -- how can we tell that there might be more?

Reference