Hi, is there a way to combine multiple text files into one large text file?
Example
I have three text files:

1.txt:
bmwboy|5

2.txt:
ashley|33

3.txt:
bob|15

How can I make this into one text file, that contains:
bmwboy|5
ashley|33
bob|15

Thanks in advance,
Bmwboy

    <?php
    
    $txt1 = file_get_contents('1.txt');
    $txt1 .= "\n" . file_get_contents('2.txt');
    $txt1 .= "\n" . file_get_contents('3.txt');
    
    $fp = fopen('newText.txt', 'w');
    if(!$fp)
        die('Could not create / open text file for writing.');
    if(fwrite($fp, $txt1) === false)
        die('Could not write to text file.');
    
    echo 'Text files have been merged.';
    
    ?>

    Quick and dirty.... if you need to make it a function, I'm sure you can figure it out 😉

    14 years later

    bpat1434
    Im trying to use this script. Works well but what I need to compile are inc/php files in which I have a lot of <?php ?> php openings and closures. Whenever the compilations reaches one of these, it automatically ends the process. It compiles the file until that point. Do you know how to solve this? Thanks!

      There's no compilation going on here. It's reading files and writing them, without any consideration about whether it's PHP or delimiter-separated variables.

      Because there's no consideration for what syntax there might be in the files, there's no guarantee that the resulting file will be syntactically valid. If you execute that there's likely to be problems.

      You'll also get problems if you try to read the files from a URL instead of a filename, because then if you're lucky you'll get the results of executing the file, not the file itself.

        Wondering if this is another X/Y problem? Maybe instead of compiling PHP files into one file, you should be include-ing or require-ing them as needed within another script?

          Well, it's working now, based on what the OP said following their posting the same question on Stack Overflow (as well as a chunk of waffle about what "compile" means), so we can probably call this closed. The sentence "I have a lot of <?php ?> php openings and closures." is worrying, though, as it suggests either a poor separation of concerns or that the desired consolidation wouldn't even make sense in the first place.

            Write a Reply...