php合并拆分大文件 yield

最好用命令行执行,没有超时时间

$v){
        //每10000条放一个文件里
        if($i == 10000){
            $i = 0;
            $n++;
        }
        $newFileName = $dir.$prefix.$n.$ext;
        file_put_contents($newFileName,$v,FILE_APPEND);
        $i++;
    }
}

/**
 * 合并多个文件
 * @param array $arrayFile
 * @param string $merageFile
 */
function merge_file($arrayFile=[],$merageFile='temp.log'){
    if(!empty($arrayFile)){
        foreach($arrayFile as $k=>$v){
            if(file_exists($v)){
                foreach(read_file($v) as $k1=>$v1){
                    file_put_contents($merageFile,$v1,FILE_APPEND);
                }
            } else {
                echo "文件".$v.'不存在!';
            }
        }
    } else {
        echo "合并数组为空!";
    }
}

/**
 * 获取目录下的所有文件
 * @param $dir
 * @return array|false
 */
function get_dir_file($dir){
    $returnArr = [];
//    $tempArr = [];
    if(!is_dir($dir)){
        return false;
    }
    $dirArr = scandir($dir);
    if(!empty($dirArr)){
        foreach($dirArr as $k=>$v){
            if($v != '.' && $v != '..' && substr($v,-4) == '.log'){
//                echo $v."
"; $file_location = $dir."/".$v; if(is_dir($file_location)){ // echo $file_location."
"; // $tmp = get_dir_file($file_location); $returnArr[$v] = get_dir_file($file_location); } else { // echo $file_location."
"; $returnArr[] = $file_location; } } } } return $returnArr; } $logDir = __DIR__."/log/"; $mergeArr = [ $logDir.'122.114.227.46.log', $logDir.'www.lishiketang.com.log', ]; //$dirArr = get_dir_file(__DIR__."/log/"); //print_r($dirArr); //合并文件 //merge_file(get_dir_file(__DIR__."/log/")); //分割文件 split_file(__DIR__."/temp.log",__DIR__."/splitlog/"); function getDir($path){ $arr = array(); $arr[] = $path; if(is_file($path)){ }else{ if(is_dir($path)){ $data = scandir($path); if(!empty($data)){ foreach ($data as $value){ if($value != '.' && $value != '..'){ $sub_path = $path."/".$value; $temp = getDir($sub_path); $arr = array_merge($temp,$arr); } } } } } return $arr; } //$path = 'lss'; //print_r(getDir(__DIR__)); //print_r($fileNameArr); //function formatBytes($bytes, $precision = 2) { // $units = array("b", "kb", "mb", "gb", "tb"); // // $bytes = max($bytes, 0); // $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); // $pow = min($pow, count($units) - 1); // // $bytes /= (1 << (10 * $pow)); // // return round($bytes, $precision) . " " . $units[$pow]; //} // // function getLines($file) { // $f = fopen($file, 'r'); // try { // while ($line = fgets($f)) { // yield $line; // } // } finally { // fclose($f); // } //} //echo round(memory_get_usage() / 1024 / 1024, 2) . ' MB'. PHP_EOL; //foreach (getLines('access.20210223') as $n => $line) { // if($n < 10){ // echo $line."
"; // $arr = explode(" ",$line); // print_r($arr); // } //} //echo round(memory_get_usage() / 1024 / 1024, 2) . ' MB'. PHP_EOL; // //function getLines2($file){ // $array = []; // $f = fopen($file, 'r'); // try { // while ($line = fgets($f)) { // $array[] = $line; // } // } finally { // fclose($f); // } // return $array; //} // echo round(memory_get_usage() / 1024 / 1024, 2) . ' MB'. PHP_EOL; $data = getLines2('access.20210223'); var_dump(count($data)); echo round(memory_get_usage() / 1024 / 1024, 2) . ' MB'. PHP_EOL;

你可能感兴趣的:(php,前端,服务器)