zoukankan      html  css  js  c++  java
  • php文件hash算法,秒传原理

    header('Content-type:text/html;Charset=UTF-8');
    
    define('blockSize', 4*1024*1024);
    
    var_dump(fileHash('test.wmv'));
    var_dump(fileHash('asdf.wmv'));
    
    function fileHash($file)
    {
        $f = fopen($file, "r");
        if (!$f) exit("open $file error");
    
        $fileSize = filesize($file);
        $buffer   = '';
        $sha      = '';
        // 一共有多少分片
        $blkcnt   = $fileSize/blockSize;
        if ($fileSize % blockSize) $blkcnt += 1;
        // 把数据装入一个二进制字符串
        $buffer .= pack("L", $blkcnt);
        if ($fileSize <= blockSize) {
            $content = fread($f, blockSize);
            if (!$content) {
                fclose($f);
                exit("read file error");
            }
            $sha .= sha1($content, TRUE);
        } else {
            for($i=0; $i<$blkcnt; $i+=1) {
                $content = fread($f, blockSize);
                if (!$content) {
                    if (feof($f)) break;
                    fclose($f);
                    exit("read file error");
                }
                $sha .= sha1($content, TRUE);
            }
            $sha = sha1($sha, TRUE);
        }
        $buffer .= $sha;
        $hash = urlSafeEncode(base64_encode($buffer));
        fclose($f);
        return array($hash, null);
    }
    
    function urlSafeEncode($data)
    {
        $find = array('+', '/');
        $replace = array('-', '_');
        return str_replace($find, $replace, $data);
    }
    

      

  • 相关阅读:
    Java/IO流
    Java实现IO通信(服务器篇)
    利用哈夫曼二叉树实现文件的压缩
    关于字符串构建,连接,查找
    线程小球
    准备造一个轮子,关于图片浏览器的
    IOS之循环引用
    ARC
    构造方法与快速创建对象
    autorelease
  • 原文地址:https://www.cnblogs.com/adtuu/p/5881131.html
Copyright © 2011-2022 走看看