note 75645 added to function.stream-wrapper-register

From: Date: Sun, 10 Jun 2007 03:10:01 +0000
Subject: note 75645 added to function.stream-wrapper-register
Groups: php.notes 
Request: Send a blank email to php-notes+get-127344@lists.php.net to get a copy of this message
In response to Anonymouse at Coward dot com: The manual says "Reading stops when up to length bytes have been read, [...] or (after opening userspace stream) when 8192 bytes have been read whichever comes first." I tested it and fread($filehandle, 4096) returns 4096 bytes, so it's working as the manual says it should. You're right when you say "8192 bytes is always passed to stream_read as count", but that doesn't mean fread will return 8192 bytes. If you call fread twice with length 4096, PHP calls stream_read passing 8192 as count on the first fread, and doesn't call it on second fread. On both cases, fread returns the correct amount of bytes. <?php class VariableStream { var $position; var $varname; function stream_open($path, $mode, $options, &$opened_path) { $url = parse_url($path); $this->varname = $url["host"]; $this->position = 0; return true; } function stream_read($count) { echo "stream_read called asking for $count bytes\n"; $ret = substr($GLOBALS[$this->varname], $this->position, $count); $this->position += strlen($ret); return $ret; } function stream_eof() { return $this->position >= strlen($GLOBALS[$this->varname]); } } stream_wrapper_register("var", "VariableStream") or die("Failed to register protocol"); $myvar = ""; $l=range('a','z'); for($i=0;$i<65536;$i++) { $myvar .= $l[array_rand($l)]; } $fp = fopen("var://myvar", "r+"); while (!feof($fp)) { $out = fread($fp,1000); echo "fread returned ",strlen($out)," bytes\n"; } fclose($fp); ?> ---- Server IP: 200.59.45.3 Probable Submitter: 201.213.144.58 ---- Manual Page -- http://www.php.net/manual/en/function.stream-wrapper-register.php Edit -- https://master.php.net/note/edit/75645 Del: integrated -- https://master.php.net/note/delete/75645/integrated Del: useless -- https://master.php.net/note/delete/75645/useless Del: bad code -- https://master.php.net/note/delete/75645/bad+code Del: spam -- https://master.php.net/note/delete/75645/spam Del: non-english -- https://master.php.net/note/delete/75645/non-english Del: in docs -- https://master.php.net/note/delete/75645/in+docs Del: other reasons-- https://master.php.net/note/delete/75645 Reject -- https://master.php.net/note/reject/75645 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#127344) next »