PHP 忒聊
綴豖
梒 43. Streams API for PHP Extension Authors
ヶ輛
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:
瞰赽 43-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.
桶跡 43-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
綴豖
れ萸
ヶ輛
Streams API for PHP Extension Authors
奻珨撰
Streams Common API Reference