Streams Basics

Using streams is very much like using ANSI stdio functions. The main difference is in how you obtain the stream handle to begin with. In most cases, you will use php_stream_open_wrapper() to obtain the stream handle. This function works very much like fopen, as can be seen from the example below:

Beispiel 63-1. simple stream example that displays the PHP home page

php_stream * stream = php_stream_open_wrapper("http://www.php.net", "rb", REPORT_ERRORS, NULL);
if (stream) {
    while(!php_stream_eof(stream)) {
        char buf[1024];
        
        if (php_stream_gets(stream, buf, sizeof(buf))) {
            printf(buf);
        } else {
            break;
        }
    }
    php_stream_close(stream);
}

The table below shows the Streams equivalents of the more common ANSI stdio functions. Unless noted otherwise, the semantics of the functions are identical.

Tabelle 63-1. ANSI stdio equivalent functions in the Streams API

ANSI Stdio FunctionPHP Streams FunctionNotes
fopenphp_stream_open_wrapperStreams includes additional parameters
fclosephp_stream_close 
fgetsphp_stream_gets 
freadphp_stream_readThe nmemb parameter is assumed to have a value of 1, so the prototype looks more like read(2)
fwritephp_stream_writeThe nmemb parameter is assumed to have a value of 1, so the prototype looks more like write(2)
fseekphp_stream_seek 
ftellphp_stream_tell 
rewindphp_stream_rewind 
feofphp_stream_eof 
fgetcphp_stream_getc 
fputcphp_stream_putc 
fflushphp_stream_flush 
putsphp_stream_putsSame semantics as puts, NOT fputs
fstatphp_stream_statStreams has a richer stat structure

Hosting by: Hurra Communications GmbH
Generated: 2007-01-26 17:57:25