PHP stream_wrapper_register
<?php
/**
* 引用:http://php.net/manual/en/function.stream-wrapper-register.php
* 把变量当成文件读写的协议
*
* Class VariableStream
*/
class VariableStream
{
private $postition; // 当前的位置
private $varname; // 变量的名字 /**
* 打开
*
* @param $path
* @param $mode
* @param $options
* @param $opened_path
* @return bool
*/
public function stream_open($path, $mode, $options, &$opened_path)
{
$url = parse_url($path); $this->varname = $url['host'];
$this->postition = 0; return true;
} /**
* 读取
*
* @param $count
* @return string
*/
public function stream_read($count)
{ $p = $this->postition;
$ret = substr($GLOBALS[$this->varname], $p,$count);
$p += $count;
return $ret;
} /**
* 写入
*
* @param $data
* @return int
*/
public function stream_write($data)
{
$v = &$GLOBALS[$this->varname];
$l = strlen($data);
$p = &$this->postition;
$v = substr($v,0,$p) .$data.substr($v,$p+=$l); return $l;
} /**
* 返回位置
*
* @return mixed
*/
public function stream_tell()
{
return $this->postition;
} /**
* 寻位
*
* @param $offset
* @param $whence
* @return bool
*/
public function stream_seek($offset, $whence)
{
$dp = $p = &$this->postition;
$l = strlen($GLOBALS[$this->varname]); switch($whence)
{
case SEEK_SET:
$p = 0;
break;
case SEEK_END:
$p = $l;
break;
default:
return false;
}
$p += $offset;
$ret = ($p >= 0 || $p <= $l);
if(!$ret)
$this->postition = $dp;
return $ret;
} public function stream_eof()
{
return strlen($GLOBALS[$this->varname])== $this->postition;
}
} // 撤销已注册的同名协议
if(in_array('var',stream_get_wrappers()))
{
stream_wrapper_unregister('var');
}
// 注册自己的协议
stream_wrapper_register('var','VariableStream') or die('Failed to register protocol'); $myvar = "";
$fp = fopen('var://myvar','r+');
if(!$fp){
die('Failed to open the file!');
}
$str = "Hello the world!";
fwrite($fp,$str,strlen($str)); echo 'The length of the string is ' .strlen($myvar) ."\n";
echo 'The value of variable ,$myvar,is :' .$myvar ."\n";
echo 'The position of the cursor is :' .ftell($fp) ."\n";
echo 'Is at the end of the file :' .(feof($fp) ? 'true':'false') ."\n";
if(fseek($fp,-16,SEEK_END))
{
echo 'Now,the position is :' .ftell($fp);
}
PHP stream_wrapper_register的更多相关文章
- 如何执行字符串的PHP代码
如何执行字符串的PHP代码 最近因项目需要,引出一个议题:如何执行字符串的php代码(php和html混写). 注:传统情况下,php代码存储在文件中,直接运行文件即可.以下讨论的情况是,如果php代 ...
- [转] 基于PHP Stream Wrapper开发有趣应用场景
PHP Stream Wrapper 原文:http://blog.sina.com.cn/s/blog_502c8cc40100k40e.html ,主要是基于SAE环境讲述相应的应用场景,本文经过 ...
- LFI、RFI、PHP封装协议安全问题学习
本文希望分享一些本地文件包含.远程文件包含.PHP的封装协议(伪协议)中可能包含的漏洞 相关学习资料 http://www.ibm.com/developerworks/cn/java/j-lo-lo ...
- LFI & RFI & PHP封装协议之安全问题研究
目录 . 文件包含的基本概念 . LFI(Local File Include) . RFI(Remote File Include) . PHP中的封装协议(伪协议).PHP的流式文件操作模式所带来 ...
- PHP 支持的协议/封装协议列表
附录 L. 支持的协议/封装协议列表 目录 文件系统 Socket HTTP 和 HTTPS FTP 和 FTPS PHP 输入/输出流 压缩流 Secure Shell 2 音频流 以下是 PHP ...
- PHP类和函数注释大全
每次要用PHP的某个功能的时候,都要去查一下,于是决定将PHP所有类和函数都整理出来,加上注释 大致实现 将php.jar文件解压,取出目录stubs 将stubs中的所有php文件中的注释去掉,并做 ...
- LFI/RFI总结
目录 0×01 文件包含简介 服务器执行PHP文件时,可以通过文件包含函数加载另一个文件中的PHP代码,并且当PHP来执行,这会为开发者节省大量的时间.这意味着您可以创建供所有网页引用的标准页眉或菜单 ...
- GeSHi Documentation
GeSHi Documentation Version 1.0.8.11 Authors: © 2004 - 2007 Nigel McNie © 2007 - 2012 Benny Baumann ...
- PHP stream相关协议及上下文选项和参数归纳
支持的协议和封装协议 PHP 带有很多内置 URL 风格的封装协议,可用于类似 fopen(). copy(). file_exists() 和 filesize() 的文件系统函数. 除了这些封装协 ...
随机推荐
- java生成随机验证码图片
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; i ...
- 第二场周赛(递归递推个人Rank赛)——题解
很高兴给大家出题,本次难度低于上一场,新生的六个题都可以直接裸递归式或者裸递推式解决,对于老生的汉诺塔3,需要找出一般式,后两题分别为裸ST算法(或线段树)/线性DP. 正确的难度顺序为 种花 角谷定 ...
- Spring Boot 入门之基础构建篇(一)
博客地址:http://www.moonxy.com 一.前言 随着 Spring 的功能越来越强,在使用 Spring 的时候,门槛也变得高了起来,诸如搭建一个基于 Spring 的 Web 程序却 ...
- charles 访问控制设置
本文参考:charles 访问控制设置 charles 访问控制设置 access control settings 访问账户设置: 这里可以配置连接到charles时的一些配置: 这个访问控制确定谁 ...
- [STL] Implement "map", "set"
练习热身 Ref: STL中map的数据结构 C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Re ...
- Transformer各层网络结构详解!面试必备!(附代码实现)
1. 什么是Transformer <Attention Is All You Need>是一篇Google提出的将Attention思想发挥到极致的论文.这篇论文中提出一个全新的模型,叫 ...
- pathlib模块
一.pathlib库官方定义 pathlib 是Python内置库,Python 文档给它的定义是 Object-oriented filesystem paths(面向对象的文件系统路径).path ...
- gcc编译命令总结
一步到位编译:gcc hello.c -o hello 预处理 -E (.i) 编译 -S (.s) 汇编-c (.o) 连接-o 预处理 gcc -E hello.c -o hello.i -E:仅 ...
- 阿里云安装RocketMQ
说明: 我的阿里云是centos 6.9 jdk 1.8.0_192-b12(安装教程参照:https://www.cnblogs.com/kingsonfu/p/9801556.html) mave ...
- .Net Core 商城微服务项目系列(十三):搭建Log4net+ELK+Kafka日志框架
之前是使用NLog直接将日志发送到了ELK,本篇将会使用Docker搭建ELK和kafka,同时替换NLog为Log4net. 一.搭建kafka 1.拉取镜像 //下载zookeeper docke ...