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";  
}  

本书共88小节:

评论 (写第一个评论)