主页 M

php分割大文件和合并文件的源代码

2016-01-05 网页编程网 网页编程网
<?php
$orgFile = 'VirtualBox.exe';
$cacheFileName = 'vbcache';

cutFile($orgFile, 10 * pow(2,20)); //10 * pow(2,20) 就等于 10M 
mergeFile('cache2vb.exe');

function cutFile($fileName,$block) {
    global $cacheFileName;
    if (!file_exists($fileName)) return false;
    $num    = 1;    
    $file   = fopen($fileName, 'rb');
    while ($content = fread($file,$block)) {
        $cacheFile = $cacheFileName . $num++ . '.dat';
        $cfile = fopen($cacheFile, 'wb');
        fwrite($cfile, $content);
        fclose($cfile);
    }
    fclose($file);
}

function mergeFile($targetFile) {
    global $cacheFileName;
    $num = 1;
    $file = fopen($targetFile, 'wb');
    while ($num > 0) {
        $cacheFile = $cacheFileName . $num++ . '.dat';
        if (file_exists($cacheFile)) {
            $cfile = fopen($cacheFile, 'rb');
            $content = fread($cfile, filesize($cacheFile));
            fclose($cfile);
            fwrite($file, $content);
        } else {
            $num = -1;
        }
    }
    fclose($file);    
}
阅读原文
阅读 4175
123 显示电脑版