Chinaunix首页 | 论坛 | 博客
  • 博客访问: 177676
  • 博文数量: 36
  • 博客积分: 2078
  • 博客等级: 大尉
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-09 17:13
文章分类

全部博文(36)

文章存档

2012年(1)

2011年(5)

2010年(9)

2009年(21)

我的朋友

分类:

2009-07-22 00:45:39

<?php
    /**
     * 函数作用:构建上传文件保存目录,
     * 本函数好处 : 文件目录分布均匀,很少出现某个目录根下文件或者目录过多而导致速度缓慢的问题,
     * 当然,这个算法也有极限,到底多少没仔细算过,此函数也可以稍作修改可以存储更多的文件,
     * 例如:加密算法改成sha1等。
     * @function FilePath
     * @param string stringMds - 需要根据哪个特定的数据进行设置,例如用户名,或者用户ID等
     * @param integer intLen - 目录名长度,默认为2个字符,值必须为1或者2的倍数,取值范围在1到32之间(此参数很重要,它决定目录分布方式)
     * @param string stringUpPath - 保存上传的基目录 默认为upload
     * 作者:七夜
     * 日期:2009-7-22 00:50:37
     */

    function FilePath( $stringMds, $intLen, $stringUpPath )
    {
        $stringPath = empty( $stringUpPath ) ? 'upload/' : $stringUpPath;
        $intLen = $intLen == 0 ? 2 : $intLen;
        for( $i=0; $i<strlen( md5( $stringMds ) ) / $intLen; $i++ )
        {
            $stringPath .= substr( md5( $stringMds ), $i*$intLen, $intLen ).'/';
            if( !is_dir( $stringPath ) )
            {
                @mkdir( $stringPath, 0777 );
            }
        }
        return $stringPath;
    }

    //以下为测试代码

    $upPath = 'upload/';
    $n = 0;
    for ( $i=0; $i<1000000; $i++ )
    {
        $p = FilePath( $i.'.php',2, $upPath );
        $files = $p.md5( $i ).'.php';
        copy( '1.php', $files );
        $n += 1;
    }
    echo '复制了'.$n.'个文件';
?>
阅读(955) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~