note 62634 added to function.stream-context-create

From: Date: Mon, 06 Mar 2006 01:31:30 +0000
Subject: note 62634 added to function.stream-context-create
Groups: php.notes 
Request: Send a blank email to php-notes+get-104875@lists.php.net to get a copy of this message
Example of a stream for reading a string passed via a context object. <?php /* ---------------------------------------- * Designed to read from a string */ class sfStreamStringRead { const PROTOCOL = 'stringread'; /* Underscore not allowed */ protected $dataPos = NULL; protected $dataBuf = NULL; protected $dataLen = NULL; function stream_open($path, $mode, $options, &$opened_path) { /* Verify context has data */ $contextOptions = stream_context_get_options($this->context); if (!isset($contextOptions[self::PROTOCOL]['data'])) { return FALSE; } $this->dataBuf = $contextOptions[self::PROTOCOL]['data']; $this->dataLen = strlen($this->dataBuf); $this->dataPos = 0; return TRUE; } function stream_read($count){ $ret = substr($this->dataBuf, $this->dataPos, $count); $this->dataPos += strlen($ret); return $ret; } function stream_eof(){ return $this->dataPos >= $this->dataLen; } function stream_tell(){ return $this->dataPos; } /* ------------------------------------------ * A few helper functions */ static function genURL() { return self::PROTOCOL . '://'; } static function genContext($dataBuf) { return stream_context_create(array( self::PROTOCOL => array( 'data' => $dataBuf, ), )); } static function open($dataBuf) { return fopen(self::genURL(),'r',FALSE,self::genContext($dataBuf)); } } stream_wrapper_register( sfStreamStringRead::PROTOCOL, 'sfStreamStringRead' ); $sp = sfStreamStringRead::open("Some String Data\n"); echo fgets($sp); fclose($sp); ?> ---- Server IP: 216.194.113.175 Probable Submitter: 68.62.208.88 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.stream-context-create.php Edit -- http://master.php.net/note/edit/62634 Del: integrated -- http://master.php.net/note/delete/62634/integrated Del: useless -- http://master.php.net/note/delete/62634/useless Del: bad code -- http://master.php.net/note/delete/62634/bad+code Del: spam -- http://master.php.net/note/delete/62634/spam Del: non-english -- http://master.php.net/note/delete/62634/non-english Del: in docs -- http://master.php.net/note/delete/62634/in+docs Del: other reasons-- http://master.php.net/note/delete/62634 Reject -- http://master.php.net/note/reject/62634 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104875) next »