/* reimplement fopen using stream */ZEND_FUNCTION(donie_stream_fopen){ php_stream *stream; char *path, *mode; int path_len, mode_len; int options = ENFORCE_SAFE_MODE|REPORT_ERRORS; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &mode, &mode_len) == FAILURE) { return; } stream = php_stream_open_wrapper(path, mode, options, NULL); if (!stream) { RETURN_FALSE; } php_stream_to_zval(stream, return_value);}
php_stream_open_wrapper()是對文件類型資源創建流的方法,此外還有基於socket的流、目錄流和特殊流三種。php_stream_to_zval()用於把流實例轉換成zval結構。
#define php_stream_open_wrapper(path, mode, options, opened) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), NULL STREAMS_CC TSRMLS_CC)#define php_stream_open_wrapper_ex(path, mode, options, opened, context) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), (context) STREAMS_CC TSRMLS_CC)
參數path是文件名或URL,mode是模式字符串,options是選項組合。php_stream_open_wrapper_ex()允許指定一個流的上下文。
options包含以下選項:
php_stream *_php_stream_xport_create(const char *name, size_t namelen, int options, int flags, const char *persistent_id, struct timeval *timeout, php_stream_context *context, char **error_string, int *error_code)
參數:
flags:
php_stream php_stream_opendir(const char *path, int options, php_stream_context *context)
php_stream *php_stream_fopen_tmpfile(void);php_stream *php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path);php_stream *php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id);php_stream *php_stream_fopen_from_file(FILE *file, const char *mode);php_stream *php_stream_fopen_from_pipe(FILE *file, const char *mode);
// 讀一個字符int php_stream_getc(php_stream *stream);// 讀取指定數量的字符size_t php_stream_read(php_stream *stream, char *buf, size_t count);// 讀取直到行末、或流末、或最多maxlen個字符char *php_stream_get_line(php_stream *stream, char *buf, size_t maxlen, size_t *returned_len);char *php_stream_gets(php_stream *stream, char *buf, size_t maxlen);// 與php_stream_get_line相同,可指定截止標記char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *returned_len, char *delim, size_t delim_len TSRMLS_DC);// 讀取一個目錄項php_stream_dirent *php_stream_readdir(php_stream *dirstream, php_stream_dirent *entry);
// 寫非阻塞流可能寫入的數據比傳入的短;_string要求傳入的字符串以NULL結尾size_t php_stream_write(php_stream *stream, char *buf, size_t count);size_t php_stream_write_string(php_stream *stream, char *stf);int php_stream_putc(php_stream *stream, int c);// 與_string不同的是會自動追加一個換行符到字符串末尾int php_stream_puts(php_string *stream, char *buf);size_t php_stream_printf(php_stream *stream TSRMLS_DC, const char *format, ...);
int php_stream_flush(php_stream *stream);
在關閉流的時候,flush會被自動調用,並且大部分無過濾的流因無內部緩沖而不需flush,所以單獨flush一般是不需要的。
int php_stream_seek(php_stream *stream, off_t offset, int whence); int php_stream_rewind(php_stream *stream); int php_stream_rewinddir(php_stream *dirstream); off_t php_stream_tell(php_stream *stream);
offset是相對於whence的位移量,whence包含:
int php_stream_stat(php_stream *stream, php_stream_statbuf *ssb);
#define php_stream_close(stream) php_stream_free((stream), PHP_STREAM_FREE_CLOSE) #define php_stream_pclose(stream) php_stream_free((stream), PHP_STREAM_FREE_CLOSE_PERSISTENT)
包含以下選項: