Split big files using PHP

后端 未结 9 1223
感动是毒
感动是毒 2020-12-06 02:05

I want to split huge files (to be specific, tar.gz files) in multiple part from php code. Main reason to do this is, php\'s 2gb limit on 32bit system.

SO I want to s

9条回答
  •  爱一瞬间的悲伤
    2020-12-06 02:41

    function split_file($source, $targetpath='/split/', $lines=1000){
    
        $i=0;
        $j=1;
        $date = date("m-d-y");
        $buffer='';
    
        $handle = fopen ($_SERVER['DOCUMENT_ROOT'].$source, "r");
    
        while (!feof ($handle)) {
            $buffer .= fgets($handle, 4096);
            $i++;
            if ($i >= $lines) {
                $fname = $_SERVER['DOCUMENT_ROOT'].$targetpath."part_".$date.$j.".txt";
    
                     $fhandle = fopen($fname, "w") or die($php_errormsg);
    
                if (!$fhandle) {
                    echo "Cannot open file ($fname)";
                    //exit;
                }
    
    
                if (!fwrite($fhandle, $buffer)) {
                    echo "Cannot write to file ($fname)";
                    //exit;
                }
                fclose($fhandle);
                $j++;
                $buffer='';
                $i=0;
                $line+=10; // add 10 to $lines after each iteration. Modify this line as required
            }
        }
        fclose ($handle);
    }
    

提交回复
热议问题