No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher une fonction PHP

Example class registered as stream wrapper

The example below implements a var:// protocol handler that allows read/write access to a named global variable using standard filesystem stream functions such as fread(). The var:// protocol implemented below, given the URL "var://foo" will read/write data to/from $GLOBALS["foo"].

Example #1 A Stream for reading/writing global variables

<?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)
    {
        
$ret substr($GLOBALS[$this->varname], $this->position$count);
        
$this->position += strlen($ret);
        return 
$ret;
    }

    function 
stream_write($data)
    {
        
$left substr($GLOBALS[$this->varname], 0$this->position);
        
$right substr($GLOBALS[$this->varname], $this->position strlen($data));
        
$GLOBALS[$this->varname] = $left $data $right;
        
$this->position += strlen($data);
        return 
strlen($data);
    }

    function 
stream_tell()
    {
        return 
$this->position;
    }

    function 
stream_eof()
    {
        return 
$this->position >= strlen($GLOBALS[$this->varname]);
    }

    function 
stream_seek($offset$whence)
    {
        switch (
$whence) {
            case 
SEEK_SET:
                if (
$offset strlen($GLOBALS[$this->varname]) && $offset >= 0) {
                     
$this->position $offset;
                     return 
true;
                } else {
                     return 
false;
                }
                break;

            case 
SEEK_CUR:
                if (
$offset >= 0) {
                     
$this->position += $offset;
                     return 
true;
                } else {
                     return 
false;
                }
                break;

            case 
SEEK_END:
                if (
strlen($GLOBALS[$this->varname]) + $offset >= 0) {
                     
$this->position strlen($GLOBALS[$this->varname]) + $offset;
                     return 
true;
                } else {
                     return 
false;
                }
                break;

            default:
                return 
false;
        }
    }

    function 
stream_metadata($path$option$var
    {
        if(
$option == STREAM_META_TOUCH) {
            
$url parse_url($path);
            
$varname $url["host"];
            if(!isset(
$GLOBALS[$varname])) {
                
$GLOBALS[$varname] = '';
            }
            return 
true;
        }
        return 
false;
    }
}

stream_wrapper_register("var""VariableStream")
    or die(
"Failed to register protocol");

$myvar "";

$fp fopen("var://myvar""r+");

fwrite($fp"line1\n");
fwrite($fp"line2\n");
fwrite($fp"line3\n");

rewind($fp);
while (!
feof($fp)) {
    echo 
fgets($fp);
}
fclose($fp);
var_dump($myvar);

?>

The above example will output:

line1
line2
line3
string(18) "line1
line2
line3
"

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-stream.streamwrapper.example-1.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut