Page 1 of 1

How to concatonate 2 files together

Posted: Sat May 22, 2010 10:22 am
by bmcconkie
I am new to PHP and would like to merge or concatonate 2 text files together. The UNIX equivalent is "cat file1.txt >> file2.txt" Can someone show me an example of how to do this? Thanks.

Re: How to concatonate 2 files together

Posted: Sat May 22, 2010 11:43 am
by AbraCadaver
If on *nix you can exec() the command:

Code: Select all

exec('cat /path/to/file1.txt >> /path/to/file2.txt');
In PHP this is one way:

Code: Select all

file_put_contents('/path/to/file1.txt', file_get_contents('/path/to/file2.txt'), FILE_APPEND);

Re: How to concatonate 2 files together

Posted: Sat May 22, 2010 10:28 pm
by Chalks
incidentally:
Allowing users to specify any portion of the string you pass to the exec, system, shell_exec, or passthru functions is a huge security risk, and you need to plan carefully in advance before allowing any of this.
From a rather interesting article.


So be careful with that method.