sanders_yao
版主
  
or2 =333
UID 30286
精华
0
积分 2504
帖子 4657
金钱 2501 喜悦币
威望 0
人脉 3
阅读权限 100
注册 2004-7-23 来自 北京 菜户营
状态 离线
|
[推荐阅读] 紧急!寻找解决方案
一直给老板做项目,最近在师父的关照下才有了点时间写自己的代码。放到这里备案,以防被删除或被当成敏感内容和谐。 终于完成了,老规矩,由于这里的php标签吃斜杠,我打个包,有兴趣的下载看一下,还请高手赐教。
<?php //================================================== // 类型: HashStore // 作者: Sanders Yao // 功能: 使用列散路径存储数据
// 属性: // @itsDir: 数据所在目录 // @itsExtra: 数据文件扩展名 // @itsSetTrigger: 保存数据触发器 // @itsGetTrigger: 获取数据触发器
// 方法: // HashStore: 构造函数 // setTrigger: 设定触发器 // getPath: 依据键名穿件并返回数据存储路径 // get: 根据键名获取数据 // set: 根据键名存储数据 // mkpath: 根据路径循环创建完整目录 // keyConvert: 键名和文件名之间的相互转换 //================================================== define("HASHSTORE_KEYCONVERT_DERICT_TOFILE", 1); define("HASHSTORE_KEYCONVERT_DERICT_TOVARIBLE", 0);
class HashStore{ var $itsDir; var $itsExtra; var $itsErrorTrigger; var $itsSetTrigger; var $itsGetTrigger; //================================================== // 方法名: HashStore($dir) // 功能: 构造函数 // 参数: // @dir: 数据存储路径 // 返回: 本类型的对象实例 //================================================== function HashStore($dir){ if(is_dir($dir)){ $this->itsDir = $dir; }else{ $this->mkpath($dir, 0777); } $this->itsExtra = ".dat"; $this->itsErrorTrigger = NULL; $this->itsSetTrigger = NULL; $this->itsGetTrigger = NULL; } //================================================== // 方法名: setTrigger($set, $get, $error) // 功能: 设定触发器 // 参数: // @set: 存储触发器 // @get: 获取触发器 // @error: 错误触发器 // 返回: 无 //================================================== function setTrigger($set = NULL, $get = NULL, $error = NULL){ $this->itsErrorTrigger = $set; $this->itsSetTrigger = $get; $this->itsGetTrigger = $error; } //================================================== // 方法名: getPath($key) // 功能: 通过键创建并返回路径 // 参数: // @key: 键值 // 返回: 路径 //================================================== function getPath($key){ $hash = md5($key); $path = $this->itsDir . $hash[0] . $hash[1] . "/" . $hash[2] . $hash[3] . "/" . $this->keyConvert($key) . "/"; if(!is_dir($path)){ $this->mkpath($path, 0777); } return $path; } //================================================== // 方法名: get($key, $cell = NULL, $func = NULL) // 功能: 通过键获取数据 // 参数: // @key: 键值 // @cell: 限定列 // @func: 调用的触发函数 置空则调用$this->itsGetTrigger // 返回: 数据 //================================================== function get($key, $cell = NULL, $func = NULL){ $path = $this->getPath($key); $data = array(); if(is_array($cell)){ foreach($cell as $v){ $var = $this->keyConvert($v); if(file_exists($path.$var.$this->itsExtra)){ $data[$v] = file_get_contents($path.$var.$this->itsExtra); } } }elseif(is_string($cell)){ $var = $this->keyConvert($cell); if(file_exists($path.$var.$this->itsExtra)){ $data[$cell] = file_get_contents($path.$var.$this->itsExtra); } }else{ $files = glob($path."*".$this->itsExtra); foreach($files as $v){ $lenPath = strlen($path); $lenVar = strlen($v) - $lenPath - strlen($this->itsExtra); $var = $this->keyConvert(substr($v, $lenPath, $lenVar), HASHSTORE_KEYCONVERT_DERICT_TOVARIBLE); $data[$var] = file_get_contents($v); } } if($func){ call_user_func($func, $key, $data); }elseif($this->itsGetTrigger){ call_user_func($this->itsGetTrigger, $key, $data); } return $data; } //================================================== // 方法名: set($key, $data, $func = NULL) // 功能: 通过键存储数据 // 参数: // @key: 键值 // @data: 数据 二维关联数组 // @func: 调用的触发函数 置空则调用$this->itsSetTrigger // 返回: Boolean //================================================== function set($key, $data, $func = NULL){ $path = $this->getPath($key); foreach($data as $k=>$v){ $filePath = $path.$this->keyConvert($k).$this->itsExtra; if(file_exists($filePath)){ if(false == @unlink($filePath)){ call_user_func($this->itsErrorTrigger, $this, "Can not delete the file({$filePath})"); } return false; } $fp = @fopen($filePath, "w"); if(false == $fp){ if($this->itsErrorTrigger){ call_user_func($this->itsErrorTrigger, $this, "Can not write the file({$filePath})"); } return false; } flock($fp, LOCK_EX); fwrite($fp, $v); fclose($fp); unset($fp); } if($func){ call_user_func($func, $key, $data); }elseif($this->itsSetTrigger){ call_user_func($this->itsSetTrigger, $key, $data); } return true; } //================================================== // 方法名: mkpath($path, $mode) // 功能: 创建路径 // 参数: // @path: 路径 // @mode: 权限 // 返回: Boolean //================================================== function mkpath($path, $mode){ if(5 == intval(phpversion())){ if(false == @mkdir($path, 0777, true)){ if($this->itsErrorTrigger){ call_user_func($this->itsErrorTrigger, $this, "Can not create the path({$path})"); } return false; } }else{ $arrayPath = explode("/", $path); $path = array_shift($arrayPath); foreach($arrayPath as $v){ $path .= "/" . $v; if(!is_dir($path) && "." != $v && ".." != $v){ if(false == @mkdir($path, $mode)){ if($this->itsErrorTrigger){ call_user_func($this->itsErrorTrigger, $this, "Can not create the path({$path})"); } return false; } @chmod($path, $mode); } } } return true; } //================================================== // 方法名: keyConvert($key, $direct = HASHSTORE_KEYCONVERT_DERICT_TOFILE) // 功能: 将键名和变量名转换为合法的目录名 可逆向转换 // 参数: // @key: 键值或文件名 // @direct:转换方向 默认由键值向文件名转换 // 返回: 合法的文件名或还原的键名 //================================================== function keyConvert($key, $direct = HASHSTORE_KEYCONVERT_DERICT_TOFILE){ $map = array( "&" => "&", "<" => "<", ">" => ">", "\"=> "\", "/" => "/", ":" => ":", "?" => "?", "*" => "*", "|" => "|", "\""=> """, "." => "." ); if($direct){ return str_replace(array_keys($map), array_values($map), $key); }else{ return str_replace(array_values($map), array_keys($map), $key); } } } ?>
附件: 您所在的用户组无法下载或查看附件
|  最近写的一些代码都会在以下地址发布,有兴趣的来批:
http://www.phpx.com/happy/thread-131368-1-1.html |
|