Tag Archives: Stream

File Wrappers

o Provide information on protocols and encodings

 can be any file wrapper

 allows for two pipelines at most – for reading & writing

o Prefix in front of a file path

file:// php://
http:// compress.zlib://
https:// compress.bzip2://
ftp:// ftps://

o Custom Wrappers

stream_wrapper_register(protocol, classname)

Registers a protocol; implementation is part of the class.

 the class implements standard functionality like reading, writing, or changing the file position
 php_user_filter is a predefined class in php and is used in conjunction with user-defined filters

stream_context_create

Stream_context_create — Creates a stream context.

Creates and returns a stream context with any options supplied in options preset.

<?php
 $opts = array(
 'http'=>array(
 'method'=>"GET",
 'header'=>"Accept-language: en\r\n" .
 "Cookie: foo=bar\r\n"
 )
 );

$context = stream_context_create($opts);
 $fp = fopen('http://www.phpcodez.com', 'r', false, $context);
 fpassthru($fp);
 fclose($fp);
?>

Stream Contexts

Streams are resources provided by PHP that we often use transparently, but which can also be very powerful tools. By learning how to harness their power, we can take our applications to a higher level.

Every stream has a implementation wrapper which has the additional code necessary to handle the specific protocol or encoding. PHP provides some built-in wrappers and we can easily create and register custom ones. We can even modify or enhance the behavior of wrappers using contexts and filters.

A context can modify or enhance the behavior of a stream

A context is a set of parameters and stream wrapper specific options

The following functions accepts a stream $context parameter.

  • fopen
  • file_get_contents
  • file