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

本书共63小节:

评论 (写第一个评论)