Concatenate files in PHP

前端 未结 5 1220
感情败类
感情败类 2021-02-07 18:49

I\'d like to know if there is a faster way of concatenating 2 text files in PHP, than the usual way of opening txt1 in a+, reading txt2 li

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-07 18:59

    I have found using *nix cat to be the most effective here, but if for whatever reason you don't have access to it, and you are concatenating large files, then you can use this line by line function. (Error handling stripped for simplicity).

    function catFiles($arrayOfFiles, $outputPath) {
    
        $dest = fopen($outputPath,"a");
    
        foreach ($arrayOfFiles as $f) {
    
            $FH = fopen($f,"r");
    
            $line = fgets($FH);
    
            while ($line !== false) {
    
                fputs($dest,$line);
    
                $line = fgets($FH);
    
            }
    
            fclose($FH);
    
        }
    
        fclose($dest);
    
    }
    

提交回复
热议问题