9. php流、公共文件、私有文件

龶龲龮龪龦龣龟龝龙龗龔龐龦龏龍龌龶龊龉drupal8齶齲齰齯齮齭齩龐齧齤齢齟

file_put_contents("public://yunke.txt","Streams test");

鼤鼡鼟鼛龪龦齰齯齮龌鼘鼘鼕鼒鼑鼍鼉鼇鼃鼂黾黽黹黸黷黳鼍鼉黲黮龌黭黬鼃黳黫黩鼍鼉鼂黧龌黥龪齧齤齢黣黟黜鼑鼍鼉鼇

黚默龲鼕黗龊黖黒黐黍黊 file_put_contents() 龪龦黆黂龌齤齢麿麽麺麹"Streams test"龪龦麵麳麲麱麯龐龦麭麫齭龌

麧麺麭麫麤麣麺齟"public://yunke.txt" ,麟麺鼍鼉麿麽鼇麭麫麝麛鼑麗鼇麝麛黣麓麒麐鼑鼇

龪麍麺麊龣龟麈麅龉麄麂鹾

黸鹻鹹 " public://yunke.txt ",黐鹷鹴龌黭龊麧鹱鹻鹹php://input龌鼃鼂龌麍麺黥龦php鹬鹪鹨龮post鹥麯龉鹢鹠

鹝鹜 鹙鹗鹓鹏鹎鹍鹊鹇齭麍麺鹃龪龦鹁鹬鹪鸽黣龉鹨龮XML黂鸯龌鸬龏public://yunke.txt鸪php://input鸨黸鸦龌麟龗鸣鸡鸠龐龦鸜鸘

麍麺php鸔鸐鸔鸌鸋齮龌鸊鸉龶龐鸆龉龲鼕齭黐鸅鹃黣麟龌鸁鷿黐鸦龲鼕黗鷾黸黍黊龌黭麟鷻鹱鷸鷶龌麺php龪鷵鷳鷯龉龐龦鷭鷩麄麂

Java 齭黬黳鷣鷡鸜鸘龌鷟鷝龊鷚C鷳鷯黍黊龌黥鼉黷鷓鷐黍黊龌麟鷌鸦鷌鸅鷋鸉ANSI C 鷁鶿IO黆黂鶹鶵

鸊鸉php鸔龪龦鹻鶱鷩鶯鸉php鶫鶨龉鶦鶥龌鷾黸鶣鸉drupal鸣齢鶡鶠龌鸁鷿龙鶝鶛麱鼑鶚鶘鶔鹁鶑鶎麟齟

鶊鼘齟 http://blog.csdn.net/u011474028/article/details/52814049

黥麺麊麭龉龐鵰鶡龌鷟鷝龊鷚鸔黸黍黊鶊鵭鵬鵨鵦麐鵢鹬鶫鶨鼡鵠鵝鵙龌鵖鵕龊黐鵔鹍鵒鼡黽龉麄麂龌龶麭鵐鵍齩龐龦鸔鸌鸋齮龉鵌鵉鶵

龪麒龲龮鵇鵃龊鵀鴾鶫鶨鴽鼑鸔鴺鴸龉鹻鶱龌鹁鼘龐鼘麟龶drupal8齭龉鴷鹃齟

鴶麺鴴 file_put_contents("public://yunke.txt","Streams test"); 鴱鴭

麟麺龶鹙鴪麭麫鴩鴦麒黽鴤鴠龐龦鴞龔yunke.txt龉麭麫龌麒黽麝麛鼑麵麳麲"Streams test"龌黥鼉龪龦鹙鴪麭麫鴩鴦龶麓麒鼇

麟麧鷿龶鴛鷩鴘鴗麭麫齭鴔鵃龌鷟鷝龊鼃黳鴔鵃龌黥鼉鴒鴏麺 \sites\default\files 龌鷟鷝龊鴌黹黽龉鴉鴆龔鼑龌鼘龐鼘麺黸麺黳龪龦麭麫鼇

drupal鴃鹃鸔龪龦鸜鸘鹁鳿麛鹙黳鸐鳻黳麭麫鷾鳺鳸鳷黚黹龉鴉鳵龌鳻黳麭麫麧鷿麝麛黣龐龦web鼟鶔鳰鴆鳮鳭url鼟鼛黣龉鳬鴉龌龪鳪麍鳧鳦鳥鳢鼑

鷟鳞龶settings.php麭麫齭鴘鴗鹙黳鸐鳻黳麭麫龉龣鴩鴦齟

$settings['file_public_path'] = "./yunke"; //鸁黳鹙鴪麭麫鳍麹麝麛黣鴛鷩鳋鴩鴦龉yunke麭麫鳊鳆龌鳄鳰鴒鴏麺 \sites\default\files
$settings['file_private_path'] = “/home/yunke”; //鲾鴗鼡鲽麈龔鲺鶣鲾鵃龌鷿鲹鲷web鼟鶔鳮鳭url鼟鼛黣麭麫龌鴘鴗齭鳄黳鲴鲰麍黷龶麂齮齭鲬鲫鳻黳鸔鸌鸋齮

鴸鸉鸔鴉黽龉鸣齢鳿麛龶齟 \core\lib\Drupal\Core\StreamWrapper 麒黽

鵃鲢鼑龐龦php鲠鲟龉鸔鸌鸋齮鲝鲚齟 PhpStreamWrapperInterface

drupal鹃龉鸌鸋齮鲝鲚鲘鵝鲗鸡黹黽龉鲝鲚鲕齟 StreamWrapperInterface

LocalStream 齟鲔鲑龉麊鳬鸔鸌鸋齮

鲐鲎鵃鲢鼑鹙鴪鸔鸌鸋齮鲍鳻黳鸔鸌鸋齮鲍鲋鲈鸔鸌鸋齮鲍鲆鹬鸔

龪鲃鸌鸋齮鹃龐龦鸌鸋齮鹎鹍齮鹁鹎鹍齟

StreamWrapperManager 齟龏鲀鼑 StreamWrapperManagerInterface 鲝鲚龌龣麈黚鹃麺鲬鲫鸔鸌鸋齮

鱿鹃鸔鸌鸋齮鹎鹍齮龉齤齢鱻鸉drupal鱺鱷齟 Drupal\Core\DrupalKernel preHandle 鴉鴆齭龌黳鷿鳆齤齢齟

// Register stream wrappers.
    $this->Container->get('stream_wrapper_manager')->register();

preHandle 鴉鴆龉鱿鹃麺龶鸦鱥 HttpKernel 鱺鱷鱡鹍齭龉鱞鱡鹍鱥齟
http_middleware.kernel_pre_handle(Drupal\Core\StackMiddleware\KernelPreHandle)
鶊鼘龝黽龉 鱜鱙鶔Drupal8鸣齢鶡鶠鱘HttpKernel鱔鱓鱒 龐麭

鷿黹麍麺麊龣龟龉鳢鵰鼑龌鱑鱏麄麂鱎齭鳺鳸鼑龐龦鸔鸌鸋齮鱍鵌龌麺龙鴴drupal8齭鲔鹪鱋鹁龌龔鼑鱉黲龌麧鷿鶛鴠鴃鹃龉龐龦鷣龌麧鷿鱅鱃鳆麟龉龏鲀齟

class yunkeWrapper  
{  
    public $context;  
    public $handle = null;  
    public $uri = null;  
    protected static $dir = "";  
    //public $dir = "dir";//可以是相对路径或绝对路径,空表示当前路径  
  
    public function dir_closedir()  
    {  
        closedir($this->handle);  
        // We do not really have a way to signal a failure as closedir() does not  
        // have a return value.  
        return true;  
    }  
  
    /** 
     * @return bool 
     */  
    public function dir_opendir($uri, $options)  
    {  
        $this->uri = $uri;  
        $this->handle = opendir($this->getLocalPath());  
        return (bool)$this->handle;  
    }  
  
    /** 
     * @return string 
     */  
    public function dir_readdir()  
    {  
        return readdir($this->handle);  
    }  
  
    /** 
     * @return bool 
     */  
    public function dir_rewinddir()  
    {  
        rewinddir($this->handle);  
        // We do not really have a way to signal a failure as rewinddir() does not  
        // have a return value and there is no way to read a directory handler  
        // without advancing to the next file.  
        return true;  
    }  
  
    /** 
     * @return bool 
     */  
    public function mkdir($uri, $mode, $options)  
    {  
        $this->uri = $uri;  
        $recursive = (bool)($options & STREAM_MKDIR_RECURSIVE);  
        if ($recursive) {  
            // $this->getLocalPath() fails if $uri has multiple levels of directories  
            // that do not yet exist.  
            $localpath = $this->getDirectoryPath() . '/' . $this->getTarget($uri);  
        } else {  
            $localpath = $this->getLocalPath($uri);  
        }  
        if ($options & STREAM_REPORT_ERRORS) {  
            return mkdir($localpath, $mode, $recursive);  
        } else {  
            return @mkdir($localpath, $mode, $recursive);  
        }  
    }  
  
    /** 
     * @return bool 
     */  
    public function rename($from_uri, $to_uri)  
    {  
        return rename($this->getLocalPath($from_uri), $this->getLocalPath($to_uri));  
    }  
  
    /** 
     * @return bool 
     */  
    public function rmdir($uri, $options)  
    {  
        $this->uri = $uri;  
        if ($options & STREAM_REPORT_ERRORS) {  
            return rmdir($this->getLocalPath());  
        } else {  
            return @rmdir($this->getLocalPath());  
        }  
    }  
  
    /** 
     * Retrieve the underlying stream resource. 
     * 
     * This method is called in response to stream_select(). 
     * 
     * @param int $cast_as 
     *   Can be STREAM_CAST_FOR_SELECT when stream_select() is calling 
     *   stream_cast() or STREAM_CAST_AS_STREAM when stream_cast() is called for 
     *   other uses. 
     * 
     * @return resource|false 
     *   The underlying stream resource or FALSE if stream_select() is not 
     *   supported. 
     * 
     * @see stream_select() 
     * @see http://php.net/manual/streamwrapper.stream-cast.php 
     */  
    public function stream_cast($cast_as)  
    {  
        return $this->handle ? $this->handle : false;  
    }  
  
    /** 
     * Closes stream. 
     */  
    public function stream_close()  
    {  
        return fclose($this->handle);  
    }  
  
    /** 
     * @return bool 
     */  
    public function stream_eof()  
    {  
        return feof($this->handle);  
    }  
  
    /** 
     * @return bool 
     */  
    public function stream_flush()  
    {  
        return fflush($this->handle);  
    }  
  
    /** 
     * @return bool 
     */  
    public function stream_lock($operation)  
    {  
        if (in_array($operation, array(  
            LOCK_SH,  
            LOCK_EX,  
            LOCK_UN,  
            LOCK_NB))) {  
            return flock($this->handle, $operation);  
        }  
  
        return true;  
    }  
  
    /** 
     * Sets metadata on the stream. 
     * 
     * @param string $path 
     *   A string containing the URI to the file to set metadata on. 
     * @param int $option 
     *   One of: 
     *   - STREAM_META_TOUCH: The method was called in response to touch(). 
     *   - STREAM_META_OWNER_NAME: The method was called in response to chown() 
     *     with string parameter. 
     *   - STREAM_META_OWNER: The method was called in response to chown(). 
     *   - STREAM_META_GROUP_NAME: The method was called in response to chgrp(). 
     *   - STREAM_META_GROUP: The method was called in response to chgrp(). 
     *   - STREAM_META_ACCESS: The method was called in response to chmod(). 
     * @param mixed $value 
     *   If option is: 
     *   - STREAM_META_TOUCH: Array consisting of two arguments of the touch() 
     *     function. 
     *   - STREAM_META_OWNER_NAME or STREAM_META_GROUP_NAME: The name of the owner 
     *     user/group as string. 
     *   - STREAM_META_OWNER or STREAM_META_GROUP: The value of the owner 
     *     user/group as integer. 
     *   - STREAM_META_ACCESS: The argument of the chmod() as integer. 
     * 
     * @return bool 
     *   Returns TRUE on success or FALSE on failure. If $option is not 
     *   implemented, FALSE should be returned. 
     * 
     * @see http://php.net/manual/streamwrapper.stream-metadata.php 
     */  
    public function stream_metadata($uri, $option, $value)  
    {  
        $target = $this->getLocalPath($uri);  
        $return = false;  
        switch ($option) {  
            case STREAM_META_TOUCH:  
                if (!empty($value)) {  
                    $return = touch($target, $value[0], $value[1]);  
                } else {  
                    $return = touch($target);  
                }  
                break;  
  
            case STREAM_META_OWNER_NAME:  
            case STREAM_META_OWNER:  
                $return = chown($target, $value);  
                break;  
  
            case STREAM_META_GROUP_NAME:  
            case STREAM_META_GROUP:  
                $return = chgrp($target, $value);  
                break;  
  
            case STREAM_META_ACCESS:  
                $return = chmod($target, $value);  
                break;  
        }  
        if ($return) {  
            // For convenience clear the file status cache of the underlying file,  
            // since metadata operations are often followed by file status checks.  
            clearstatcache(true, $target);  
        }  
        return $return;  
    }  
  
    /** 
     * @return bool 
     */  
    public function stream_open($uri, $mode, $options, &$opened_path)  
    {  
        $this->uri = $uri;  
        $path = $this->getLocalPath();  
        $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @  
            fopen($path, $mode);  
  
        if ((bool)$this->handle && $options & STREAM_USE_PATH) {  
            $opened_path = $path;  
        }  
  
        return (bool)$this->handle;  
    }  
  
    /** 
     * @return string 
     */  
    public function stream_read($count)  
    {  
        return fread($this->handle, $count);  
    }  
  
    /** 
     * Seeks to specific location in a stream. 
     * 
     * This method is called in response to fseek(). 
     * 
     * The read/write position of the stream should be updated according to the 
     * offset and whence. 
     * 
     * @param int $offset 
     *   The byte offset to seek to. 
     * @param int $whence 
     *   Possible values: 
     *   - SEEK_SET: Set position equal to offset bytes. 
     *   - SEEK_CUR: Set position to current location plus offset. 
     *   - SEEK_END: Set position to end-of-file plus offset. 
     *   Defaults to SEEK_SET. 
     * 
     * @return bool 
     *   TRUE if the position was updated, FALSE otherwise. 
     * 
     * @see http://php.net/manual/streamwrapper.stream-seek.php 
     */  
    public function stream_seek($offset, $whence = SEEK_SET)  
    {  
        // fseek returns 0 on success and -1 on a failure.  
        // stream_seek   1 on success and  0 on a failure.  
        return !fseek($this->handle, $offset, $whence);  
    }  
  
    /** 
     * Change stream options. 
     * 
     * This method is called to set options on the stream. 
     * 
     * @param int $option 
     *   One of: 
     *   - STREAM_OPTION_BLOCKING: The method was called in response to 
     *     stream_set_blocking(). 
     *   - STREAM_OPTION_READ_TIMEOUT: The method was called in response to 
     *     stream_set_timeout(). 
     *   - STREAM_OPTION_WRITE_BUFFER: The method was called in response to 
     *     stream_set_write_buffer(). 
     * @param int $arg1 
     *   If option is: 
     *   - STREAM_OPTION_BLOCKING: The requested blocking mode: 
     *     - 1 means blocking. 
     *     - 0 means not blocking. 
     *   - STREAM_OPTION_READ_TIMEOUT: The timeout in seconds. 
     *   - STREAM_OPTION_WRITE_BUFFER: The buffer mode, STREAM_BUFFER_NONE or 
     *     STREAM_BUFFER_FULL. 
     * @param int $arg2 
     *   If option is: 
     *   - STREAM_OPTION_BLOCKING: This option is not set. 
     *   - STREAM_OPTION_READ_TIMEOUT: The timeout in microseconds. 
     *   - STREAM_OPTION_WRITE_BUFFER: The requested buffer size. 
     * 
     * @return bool 
     *   TRUE on success, FALSE otherwise. If $option is not implemented, FALSE 
     *   should be returned. 
     */  
    public function stream_set_option($option, $arg1, $arg2)  
    {  
        trigger_error('stream_set_option() not supported for local file based stream wrappers',  
            E_USER_WARNING);  
        return false;  
    }  
  
    /** 
     * @return array 
     */  
    public function stream_stat()  
    {  
        return fstat($this->handle);  
    }  
  
    /** 
     * @return int 
     */  
    public function stream_tell()  
    {  
        return ftell($this->handle);  
    }  
    /** 
     * Truncate stream. 
     * 
     * Will respond to truncation; e.g., through ftruncate(). 
     * 
     * @param int $new_size 
     *   The new size. 
     * 
     * @return bool 
     *   TRUE on success, FALSE otherwise. 
     */  
    public function stream_truncate($new_size)  
    {  
        return ftruncate($this->handle, $new_size);  
    }  
  
    /** 
     * @return int 
     */  
    public function stream_write($data)  
    {  
        return fwrite($this->handle, $data);  
    }  
  
    /** 
     * @return bool 
     */  
    public function unlink($uri)  
    {  
        $this->uri = $uri;  
        return unlink($this->getLocalPath());  
    }  
  
    /** 
     * @return array 
     */  
    public function url_stat($uri, $flags)  
    {  
        $this->uri = $uri;  
        $path = $this->getLocalPath();  
        // Suppress warnings if requested or if the file or directory does not  
        // exist. This is consistent with PHP's plain filesystem stream wrapper.  
        if ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) {  
            return @stat($path);  
        } else {  
            return stat($path);  
        }  
    }  
  
  
    /****************************************************以上为php流类原型定义的方法,以下为自定义方法*****************************************/  
    /** 
     * 设置流包装器需要操作的主目录 
     */  
    public static function setDirectoryPath($dir = '')  
    {  
        if (empty($dir)) {  
            self::$dir = "./";  
        }  
        if (!is_string($dir)) {  
            trigger_error('DirectoryPath must be string', E_USER_WARNING);  
        }  
        self::$dir = $dir;  
    }  
    /** 
     * 得到流包装器需要操作的主目录 
     */  
    public function getDirectoryPath()  
    {  
        self::$dir = empty(trim(self::$dir)) ? "./" : (self::$dir);  
        /* 
        if (!is_dir(self::$dir)) { 
        mkdir(self::$dir, 0777, true); 
        } 
        //为了方便在实用程序中可以这样做,为标准起见不这样,建立目录不应该是工具库的责任 
        */  
        return self::$dir;  
    }  
    /** 
     * 得到本地文件路径 
     */  
    protected function getLocalPath($uri = null)  
    {  
        if (!isset($uri)) {  
            $uri = $this->uri;  
        }  
        $path = $this->getDirectoryPath() . '/' . $this->getTarget($uri);  
  
        // In PHPUnit tests, the base path for local streams may be a virtual  
        // filesystem stream wrapper URI, in which case this local stream acts like  
        // a proxy. realpath() is not supported by vfsStream, because a virtual  
        // file system does not have a real filepath.  
        if (strpos($path, 'vfs://') === 0) {  
            return $path;  
        }  
        $realpath = realpath($path);  
        if (!$realpath) {  
            // This file does not yet exist.  
            $realpath = realpath(dirname($path)) . '/' . basename($path);  
        }  
        $directory = realpath($this->getDirectoryPath());  
        if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) {  
            return false;  
        }  
  
        return $realpath;  
    }  
  
    /** 
     * 得到目标 
     */  
    protected function getTarget($uri = null)  
    {  
        if (!isset($uri)) {  
            $uri = $this->uri;  
        }  
  
        list(, $target) = explode('://', $uri, 2);  
  
        // Remove erroneous leading or trailing, forward-slashes and backsl ashes.  
        return trim($target, '\/');  
    }  
  
}  
  
stream_wrapper_register("yunke", "yunkeWrapper"); //注册yunke流包装器  
if (!is_dir("yunke://a/b")) {  
    mkdir("yunke://a/b", 0777, true); //创建目录是开发者用户的责任  
}  
if (file_put_contents("yunke://a/b/wrapperTest.txt", "it is a test by:yunke time20161014") !== false) {  
    echo "ok";  
} else {  
    echo "err";  
}  
  
$dir = "yunkenew/subdir"; //再次设定yunke流主目录  
yunkeWrapper::setDirectoryPath($dir);  
if (!is_dir($dir)) {  
    mkdir($dir, 0777, true); //创建yunke流的主目录是开发者用户的责任  
}  
if (file_put_contents("yunke://wrapperTest.txt", "it is a test by:yunke time20161015") !== false) {  
    echo "ok";  
} else {  
    echo "err";  
}  

本书共71小节:

评论 (写第一个评论)