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

      

  • 相关阅读:
    MRC和ARC混合开发
    创建静态库.a
    IOS 数据存储之 FMDB 详解
    AFNETWorking的使用
    日历demo
    iOS开发系列--通知与消息机制
    paypal支付说明
    支付宝路径的问题
    iOS开发多线程篇—GCD介绍
    回调的具体最弱智的解释
  • 原文地址:https://www.cnblogs.com/adtuu/p/5881131.html
Copyright © 2011-2022 走看看