文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>小熊写的 文件目录的哈希存储

小熊写的 文件目录的哈希存储

时间:2007-08-23  来源:Givemefive

<?php
/**
 * 基础 HASH 结构数
 *
 * @Author: baijiachnag
 * @Package: core of the class
 * @Time: 200708
 * @Corpyright:
 */

/**
 * 说明:本类是基础 HASH 结构数的操作类
 * 能够实现丢 HASH 的创建 查询 删除等操作
 * 默认 HASH 层数为 3层 可以随需要修改
 */
Class Hash
{
    public $hash_hmac = 32;
    public $hash_opt = 3;
    public $base_dir = '/usr/local/www/Xml/';
    
    /**
     * 构造函数
     */
    public function __construct()
    {
    }

    /**
     * 创建 HASH 结构函数
     *
     * 说明:通过数字 HASH 算法 确定 HASH 结构目录 并且检查目录并创建
     * 参数:$id_array array ID 数字的数组
     * 返回:BOOL TRUE OF FALSE
     */
    public function createHashDir($id_array)
    {
        if(0 >= sizeof($id_array) OR false == is_array($id_array)){
            exit('Hash id array is not exists!');
        }
        foreach($id_array as $value){
            $id = $value;
            $hash_tree = $base_dir = NULL;
            $hash_tree = $this->_hash($id);
            $base_dir = $this->base_dir;
            foreach($hash_tree as $value){
                $base_dir = $base_dir . $value . '/';
                if(false == is_dir($base_dir)){
                    mkdir($base_dir, 0777);
                }
            }
        }
        return;
    }

    /**
     * 删除 HASH 结构
     *
     * 说明:通过算 HASH 结构 确认删除
     * 参数:$id INT 要删除的数字
     * 返回:BOOL TRUE OR FALSE
     */
    public function delHashDir($id)
    {
        if(false == $this->chkID($id)){
            $this->_error('Get the hash id, because of the numeric of the id.');
        }
        $base = $this->base_dir . implode('/', $this->_hash($id)) . '/';
        $fp = popen('rm -rf ' . $base, 'r');
        $re = fgets($fp, 2096);
        pclose($fp);
    }

    /**
     * 获得 HASH 目录
     *
     * 说明:通过数字算法算出 HASH 目录结构
     * 参数:$id int ID 数字变量
     * 返回:STRING HASH 结构字符串
     */
    public function getHashDir($id)
    {
        if(false == $this->chkID($id)){
            $this->_error('Get the hash id, because of the numeric of the id.');
        }
        return $this->base_dir . implode('/', $this->_hash($id)) . '/';
    }

    /**
     * 计算 HASH 结构
     *
     * 说明:用 32 递归进阶结构算法 得出3层 HASH 结构
     * 参数:$id int 要计算的数字
     * 返回:array 返回结构数组
     */
    public function _hash($id)
    {
        $id = intval($id);
        $hash_tree = Array();
        for($i = 0; $i < $this->hash_opt; $i++){
            $hash_tree[] = ceil($id / pow(32, $i)) % 32;
        }
        if(0 < sizeof($hash_tree)){
            $hash_tree[] = $id;
            return $hash_tree;
        }
        return false;
    }

    /**
     * 数字检查
     *
     * 说明:对数字进行检查
     * 参数:$int INT 要检查的数字
     * 返回:BOOL TRUE OR FALSE
     */
    public function chkID($int)
    {
        return eregi('[0-9]', $int);
    }

    /**
     * DEBUG
     *
     * 说明:错误处理
     * 参数:错误信息
     * 返回:NULL
     */
    public function _error($message)
    {
        $mess = true == empty($message) ? '不指明错误' : strval(trim($message));
        echo "";
        exit();
    }
}//end class


?>

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载