PHP Append One File To Another

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
jstorm13
Forum Newbie
Posts: 12
Joined: Wed Oct 10, 2007 12:40 pm

PHP Append One File To Another

Post by jstorm13 »

Hello:

I have a php file, when it is executed I would like it to append one file to another.

Can anyone tell me the command for this. I think it might be possible to use the system
command and cat temp.txt >> file.txt but I can't seem to get the statement correct.

Can anyone help me out?

temp.txt file needs to be appended to file.txt and I want to do this inside my PHP code.

thanks,
Jason
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you use the filesystem functions in this situation?
jstorm13
Forum Newbie
Posts: 12
Joined: Wed Oct 10, 2007 12:40 pm

Post by jstorm13 »

I tried something like

Code: Select all

system("cat $temp >> $main");
but it did not work. the files will change on a daily basis. the system command would work
if the files were static.

Code: Select all

system("cat temp.txt >> main.txt");
since the files change daily, that is why i am looking for a system solution or something, but
i think the system command will only accept two " " which prevents me from using $temp or
something like that.

thanks for the help.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What I am saying is using something like file(), fopen(), fputs(), etc.
jstorm13
Forum Newbie
Posts: 12
Joined: Wed Oct 10, 2007 12:40 pm

Post by jstorm13 »

yes, i can use those, so you think that is the most simple way to do it?

open both files, one for reading and one for append

use a while statement not end of file and fgets, then fputs??

i was thinking there might be a one line command to do this, but if not, i can just do it

the other way.

thanks
ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

There must be i used to do it in Dos using copy command from several files to one file
so there must be a Linux alternative just search for something similar to Dos approach
jstorm13
Forum Newbie
Posts: 12
Joined: Wed Oct 10, 2007 12:40 pm

Post by jstorm13 »

I just did it another way.

Code: Select all

fputs($fp, "cat ".$temp." >> ".$main);

and

system(". batch.bat &");
i just thought there might be a one line with system() instead of a few.

thanks guys..
Post Reply