简体   繁体   English

PHP流包装器错误

[英]PHP stream wrapper error

I'm trying to write a "Identity Stream Wrapper". 我正在尝试编写“身份流包装器”。 The first purpose is to make PHP transit through this wrapper which will make exactly the same things that PHP would do by itself. 第一个目的是使PHP通过该包装进行传递,这将使PHP本身执行完全相同的操作。 The second is to modify this wrapper to enable decisions making. 第二个是修改此包装器以支持决策。

So the first work was to map stream_open() to fopen(), stream_write() to fwrite(), and so on. 因此,第一个工作是将stream_open()映射到fopen(),将stream_write()映射到fwrite(),依此类推。 fopen() mapping seems to work fine, but when I try to call fwrite(), the internal resource of the wrapper turns to boolean for some reason, and the file could not be written. fopen()映射似乎可以正常工作,但是当我尝试调用fwrite()时,由于某种原因,包装器的内部资源变为布尔值,并且无法写入文件。 The same thing occurs with fread(). fread()也会发生相同的情况。

Can anyone explain me why and how to avoid/work around ? 谁能解释我为什么以及如何避免/解决问题? Thanks... 谢谢...

Here is the output of my script : 这是我的脚本的输出:

36: resource(6) of type (stream)
60: resource(7) of type (stream)
49: bool(true)
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in /var/www/projects/stream/test.php on line 51
PHP Stack trace:
PHP   1. {main}() /var/www/projects/stream/test.php:0
PHP   2. fwrite() /var/www/projects/stream/test.php:62
PHP   3. IdentityStreamWrapper->stream_write() /var/www/projects/stream/test.php:62
PHP   4. fwrite() /var/www/projects/stream/test.php:51
63: resource(7) of type (stream)

Here is its code (not all by enough to reproduce) : 这是它的代码(还不足以重现):

<?php

class IdentityStreamWrapper {
    var $fileHd;
    var $fileName;

    function __construct() {
      static::unwrap();
    }

    function __destruct() {
      static::wrap();
    }

    static function unwrap() {
        stream_wrapper_restore("file");
    }

    static function wrap() {
        stream_wrapper_unregister("file");
        stream_wrapper_register("file", "IdentityStreamWrapper");   
    }

    function stream_open($path, $mode, $options, &$opened_path)
    {
        if ($options >0) {
          $tOptions = $options;
          $tOptions = ($tOptions > STREAM_USE_PATH) ? ($tOptions - STREAM_REPORT_ERRORS) : $tOptions;
          if ($tOptions === STREAM_USE_PATH) {
            $opened_path = realpath($path);
          }
        }
        $this->fileName = $path;
        $this->fileHd = fopen($path,$mode);
        $this->position = 0;
echo __LINE__.": ";
var_dump($this->fileHd);
        return $this->fileHd;
    }

    function stream_write($data)
    {
echo __LINE__.": ";
var_dump($this->fileHd);
        $ret = fwrite($this->fileHd,$data);
        return $ret;
    }

}

IdentityStreamWrapper::wrap();

$fp = fopen("test.txt", "w+");
echo __LINE__.": ";
var_dump($fp);
fwrite($fp, "line1\n");
echo __LINE__.": ";
var_dump($fp);

IdentityStreamWrapper::unwrap();

?>

The method stream_open has to return a boolean, not a resource as fopen(). stream_open方法必须返回一个布尔值,而不是作为fopen()的资源。 That's all to fix the class. 这就是解决问题的全部方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM