How to concatonate 2 files together

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bmcconkie
Forum Newbie
Posts: 1
Joined: Sat May 22, 2010 10:16 am

How to concatonate 2 files together

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to concatonate 2 files together

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: How to concatonate 2 files together

Post 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.
Post Reply