How can PHP be used to concatenate the contents of multiple log files into a single file efficiently?

To concatenate the contents of multiple log files into a single file efficiently using PHP, you can open each log file, read its contents, and append them to a single output file. This can be achieved by iterating over each log file, reading its contents, and writing them to the output file. This approach ensures that the contents of all log files are combined into a single file in an efficient manner.

$logs = ['log1.txt', 'log2.txt', 'log3.txt'];
$outputFile = 'combined_logs.txt';

// Open the output file in append mode
$outputHandle = fopen($outputFile, 'a');

foreach ($logs as $log) {
    // Open each log file for reading
    $logHandle = fopen($log, 'r');
    
    // Read and write the contents of each log file to the output file
    while (!feof($logHandle)) {
        fwrite($outputHandle, fgets($logHandle));
    }
    
    // Close the log file handle
    fclose($logHandle);
}

// Close the output file handle
fclose($outputHandle);