PHP stream operations

Source: Internet
Author: User
PHP stream operations

/* 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 () is a method for stream creation for file-type data sources. In addition, there are three types of stream based on socket, object stream, and special stream. Php_stream_to_zval () is used to convert the stream entity into a zval structure.

Upload file-type streams

#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 indicates the file name or URL, mode indicates the mode string, and options indicates the combination of options. Php_stream_open_wrapper_ex () allows you to specify the context of a stream.

Options includes the following options:

  • USE_PATH: use include_path in ini to the corresponding path. This option is used when the third digit of the fopen () parameter is set to True.
  • STREAM_USE_URL: only the specified URL can be opened. % file: //, php: //, compress. zlib: // % the local URL will be corrupted.
  • ENFORCE_SAFE_MODE: the safe_mode takes effect only when the selected mode is set and the safe_mode in ini is enabled. if this mode is not set, it does not matter whether or not the ini is enabled.
  • REPORT_ERRORS: if an outbound listener is enabled, the upstream listener information is generated.
  • STREAM_MUST_SEEK: not all streams allow seek. if this option is set and the stream does not allow seek, the package generator will not open the stream.
  • STREAM_WILL_CAST: If this parameter is set, the stream can be converted to posix or stdio file descriptors. if the stream cannot be streamed, it can be lost before the start of IO.
  • STREAM_ONLY_GET_HEADERS: this parameter is used by the http package publisher. only the metadata of the data source is retrieved, and the content is not retrieved.
  • STREAM_DISABLE_OPEN_BASEDIR: when open_basedir in ini is enabled, set this parameter to skip open_basedir lookup.
  • STREAM_OPEN_PERSISTENT: both stream and related resources are required to be created as persistent data.
  • IGNORE_PATH: do not search from include_path.
  • IGNORE_URL: only local files can be opened.
Streams of the built-in sequence type

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)

Metric data:

  • Name: URL.
  • Options: number of records, which is the same as php_stream_open_wrapper.
  • Flags: Combination of STREAM_XPORT_CLIENT or STREAM_XPORT_SERVER and other STREAM_XPORT _ * constants.
  • Persistent_id: specifies the value of persistence. If this parameter is set, the stream will persist for multiple times.
  • Timeout: set NULL to use the value set in ini.
  • Errstr: used for sending outgoing neighbors. The initial value should be NULL. if there is outgoing neighbors, the principal party has any internal memory used for storing the principal information.
  • Errcode: too many requests.

Flags:

  • STREAM_XPORT_CLIENT: works as a client and initiates a connection to the client.
  • STREAM_XPORT_SERVER: works as a server and accepts failover.
  • STREAM_XPORT_CONNECT: the same time when STREAM_XPORT_CONNECT is set up for the primary node. Otherwise, you need to use php_stream_xport_connect () for manual failover ().
  • STREAM_XPORT_CONNECT_ASYNC: initiate a failover step.
  • STREAM_XPORT_BIND: bind the transmission stream to a local resource. when the server transmits the stream, this will enable the information such as the port, path, or specific endpoint identifier of the transport stream to be prepared for the accept connection.
  • STREAM_XPORT_LISTEN: % Listen for inbound connections on the bound transport endpoint. this is typically used with stream-based transports such as tcp: //, ssl: //, and unix: // %.
Create a category-based stream

php_stream php_stream_opendir(const char *path, int options, php_stream_context *context) 

Creating special Stream types

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);

Ghost stream

// Specify a character int php_stream_getc (php_stream * stream); // Obtain the specified number of characters size_t php_stream_read (php_stream * stream, char * buf, size_t count ); // extract until the end of the row, or the end of the stream, or a maximum of maxlen characters 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); // same as php_stream_get_line, you can specify the deadline for remembering char * php_stream_get_record (php_stream * stream, size_t maxlen, size_t * returned_len, char * delim, size_t delim_len TSRMLS_DC); // Retrieve a specific sequence using * php_stream_readdir (php_stream * dirstream, php_stream_dirent * entry );

Ghost stream

// The number of bytes that a non-blocking stream may be written into is shorter than the number of bytes that a non-blocking stream is written into. _ string requires that the string to be written in end with 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 ); // unlike _ string, it automatically appends a response line to int php_stream_puts (php_string * stream, char * buf) at the end of the string; size_t php_stream_printf (php_stream * stream TSRMLS_DC, const char * format ,...);

int php_stream_flush(php_stream *stream); 

When a stream is closed, flush will be automatically used, and most of the endless streams do not need flush because there is no internal traffic, therefore, simple volume flush is generally not required.

Address

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 is the displacement relative to whence. whence includes:

  • SEEK_SET: start of the file. When the offet parameter is set to the parameter value, it is regarded as a parameter and cannot be used. If the offset value exceeds the limit of the file, it may cause a limit, or the file is increased.
  • SEEK_CUR: the front position.
  • SEEK_END: end of the file. Offset is usually ignore, and the row of positive value is delayed due to the actual stream.
Obtain the out-of-quota information for the token.

int php_stream_stat(php_stream *stream, php_stream_statbuf *ssb); 

Stream

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

The options include:

  • PHP_STREAM_FREE_CALL_DTOR: use php_stream-> ops-> close when streaming occurs.
  • PHP_STREAM_FREE_RELEASE_STREAM: php_stream_wrapper-> ops-> stream_close
  • PHP_STREAM_FREE_PRESERVE_HANDLE: php_stream-> ops-> close non-linear handle
  • PHP_STREAM_FREE_RSRC_DTOR: used for garbage collection in the stream content resource Source list
  • PHP_STREAM_FREE_PERSISTENT: used for persistent streams. the result of all operations is valid for multiple times.
  • PHP_STREAM_FREE_CLOSE: the combination of CALL_DTOR and RELEASE_STREAM, which is used for the regular selection of non-persistent streams.
  • PHP_STREAM_FREE_CLOSE_CASTED: combination of CLOSE and PRESERVE_HANDLE
  • PHP_STREAM_FREE_CLOSE_PERSISTENT: combination of CLOSE and PERSISTENT, used for the regular layout of PERSISTENT streams

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.