PHP: Streams Basics - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Streams as Resources Streams API for PHP Extension Authors

Last updated: Fri, 26 Jan 2007

view this page in

# 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:

Пример 44-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.

Таблица 44-1. ANSI stdio equivalent functions in the Streams API

ANSI Stdio Function | PHP Streams Function | Notes
--- | --- | ---
fopen | php_stream_open_wrapper | Streams includes additional parameters
fclose | php_stream_close |
fgets | php_stream_gets |
fread | php_stream_read | The nmemb parameter is assumed to have a value of 1, so the prototype looks more like read(2)
fwrite | php_stream_write | The nmemb parameter is assumed to have a value of 1, so the prototype looks more like write(2)
fseek | php_stream_seek |
ftell | php_stream_tell |
rewind | php_stream_rewind |
feof | php_stream_eof |
fgetc | php_stream_getc |
fputc | php_stream_putc |
fflush | php_stream_flush |
puts | php_stream_puts | Same semantics as puts, NOT fputs
fstat | php_stream_stat | Streams has a richer stat structure

add a note User Contributed Notes
Streams Basics
net_navard at yahoo dot com
08-Dec-2005 07:22
`Hello firends
In crude terms, streams are chunks of data, which are carried out by low-level sockets. When you open a
connection to a host, (Web server, mail server or
whatever), in fact you're opening a socket to that
host and then sending out streams (chunks of data)
either you're sending a request or receiving a response.
Streams are the flow of data that's travelling within
the socket
(think about a socket like a logical �not physical-
pipeline handled by the operating system,
A pipeline is essential just a connection For example ,When we open file by fopen() ,It creates a connection or a pipeline on that file ,so that we can write or read data on that file.
Data in a stream flow along one of tow pipelines:
- Data sent down s stream from your PHP script to the destination file or network server flows the write pipeline.
- Data retrieved from the file or network server flows up the read pipeline. )
and transport are the protocols used to carry out the
data, such as TCP. Also metadata is additional
information that must be handled by sockets. Let's say
you're opening a file with PHP, and this operation
returns a handle or pointer to that file. In this
case, when reading from the file, its contents will be
read as chunks or packets of data (streams), while the
respective pointer, the name of the file, size and so
forth is metadata. Of course, all this data set will
be transported trough a socket via a transport such as
TCP.
With Regards
Amir Hossein Estakhrian`
add a note
Streams as Resources Streams API for PHP Extension Authors

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST