<?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);
}

最新文章

  1. Docker入门教程(二)命令
  2. js 过滤敏感词
  3. 启动Hive报错
  4. windows docker测试二 下载container
  5. 多准则决策模型-TOPSIS评价方法-源码
  6. 给定a、b两个文件,各存放50亿个url,每个url各占用64字节,内存限制是4G,如何找出a、b文件共同的url?
  7. 【面试题030】最小的k个数
  8. cardsui-for-android
  9. 【转】Mac和iOS开发资源汇总—更新于2013-07-19
  10. Linux操作系统搭建JDK开发环境
  11. hdu 2504
  12. TortoiseSVN 文件关联图标不显示的解决方法
  13. 无状态TCP的ip_conntrack
  14. Spring + Spring MVC + Hibernate
  15. javascript中 __proto__与prorotype的理解
  16. Hadoop 错误归档库
  17. Java对象的内存布局以及对象所需内存大小计算详解
  18. TDX指标的理解与改造(价格到达指标线提醒)
  19. Typora 快捷键
  20. eclipse中web项目没有run on server

热门文章

  1. NUMA导致的MySQL服务器SWAP问题分析
  2. Hive中的数据库(Database)和表(Table)
  3. 大数据平台搭建 - Mysql在linux上的安装
  4. Spring 7大模块的解说
  5. 06 (OC)* iOS中UI类之间的继承关系
  6. 【linux】【redis】redis安装及开启远程访问
  7. 浅谈Linux进程管理
  8. 第八届蓝桥杯java b组第三题
  9. Android系统开发实务实训
  10. centos 升级