Concatenate files using PHP

Method 1: Using file_get_contents and file_put_contents

<?php
$files = ['file1.txt', 'file2.txt', 'file3.txt'];
$outputFile = 'output.txt';

$content = '';
foreach ($files as $file) {
    $content .= file_get_contents($file);
}

file_put_contents($outputFile, $content);
?>

Method 2: Using fopen, fread, and fwrite

<?php
$files = ['file1.txt', 'file2.txt', 'file3.txt'];
$outputFile = 'output.txt';

$outputHandle = fopen($outputFile, 'w');

foreach ($files as $file) {
    $inputHandle = fopen($file, 'r');
    while ($line = fread($inputHandle, 8192)) {
        fwrite($outputHandle, $line);
    }
    fclose($inputHandle);
}

fclose($outputHandle);
?>

Method 3: Using file and implode

<?php
$files = ['file1.txt', 'file2.txt', 'file3.txt'];
$outputFile = 'output.txt';

$content = '';
foreach ($files as $file) {
    $content .= implode("", file($file));
}

file_put_contents($outputFile, $content);
?>

4: File plus input text

This assumes a separate htm file with an input field and POST to a php file

<?php
// Define the file paths
$file1 = 'path/to/first_file.txt';
$file2 = 'path/to/second_file.txt';

// Read the contents of the first file
$content1 = file_get_contents($file1);

// Get the user input (assuming it's passed via a POST request)
$userInput = isset($_POST['user_input']) ? $_POST['user_input'] : '';

// Read the contents of the second file
$content2 = file_get_contents($file2);

// Concatenate the contents
$finalContent = $content1 . $userInput . $content2;

// Output the final content
echo $finalContent;
?>