Can someone tell me how to concatonate 2 files together in PHP>
The UNIX equivalent is: cat file1.txt >> file2.txt

Recommended Answers

All 2 Replies

$newfilecontents=file_get_contents('file1.txt').file_get_contents('file2.txt');

If you'll be reading large files, it is better to use fopen() and fwrite() equivalents, just like how the `cat file1.txt >> file2.txt` is actually reading/writing streams instead of buffering the whole file to memory.

Example: appends file1.txt to file2.txt

if ($fp = fopen('file2.txt', 'a')) {
  if ($fp2 = fopen('file1.txt', 'r')) {
    while(!feof($fp2)) fwrite($fp, fread($fp2, 4096));
    fclose($fp2);
  }
  fclose($fp);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.