Juniorflip Posted October 18, 2006 Share Posted October 18, 2006 Hopefully this is a simple function to do but I am looking to join 3 external file and output to a single fileexampleheader.txt = hellobody.txt = welcomefooter.txt = goodbyeI need a php function that will open all three text file and print the value to a new text fileexamplegreeting.txt = hello welcome goodbyeIs this possible to do?Thanks in advance Link to comment https://forums.phpfreaks.com/topic/24282-join-external-files/ Share on other sites More sharing options...
btherl Posted October 18, 2006 Share Posted October 18, 2006 This should work[code]$header = get_file_contents('header.txt');$body = get_file_contents('body.txt');$footer = get_file_contents('footer.txt');$greeting_fp = fopen('greeting.txt', 'w');fwrite($greeting_fp, $header);fwrite($greeting_fp, $body);fwrite($greeting_fp, $footer);fclose($greeting_fp);[/code]It's better if you check every function call for errors, like[code]if ($header === false) die("Couldn't read header.txt\n");[/code] Link to comment https://forums.phpfreaks.com/topic/24282-join-external-files/#findComment-110447 Share on other sites More sharing options...
Juniorflip Posted October 18, 2006 Author Share Posted October 18, 2006 So if I wanted to make a function it would go like this[code]Function create_greet {$header = get_file_contents('header.txt');$body = get_file_contents('body.txt');$footer = get_file_contents('footer.txt');if ($header === false) die("Couldn't read header.txt\n");$greeting_fp = fopen('greeting.txt', 'w');fwrite($greeting_fp, $header);fwrite($greeting_fp, $body);fwrite($greeting_fp, $footer);fclose($greeting_fp);}create_greet();[/code] Link to comment https://forums.phpfreaks.com/topic/24282-join-external-files/#findComment-110454 Share on other sites More sharing options...
btherl Posted October 18, 2006 Share Posted October 18, 2006 Yes.. and for every other function too.It may seem like a lot of work, but it saves a lot of debugging work later.[code]Function create_greet() {$header = get_file_contents('header.txt');if ($header === false) die("Couldn't read header.txt\n");$body = get_file_contents('body.txt');if ($body === false) die("Couldn't read body.txt\n");$footer = get_file_contents('footer.txt');if ($footer === false) die("Couldn't read footer.txt\n");$greeting_fp = fopen('greeting.txt', 'w');if ($greeting_fp === false) die("Couldn't open greeting.txt\n");fwrite($greeting_fp, $header);fwrite($greeting_fp, $body);fwrite($greeting_fp, $footer);fclose($greeting_fp);}create_greet();[/code]It's not so essential to check fwrite() and fclose(), since they very rarely fail. Link to comment https://forums.phpfreaks.com/topic/24282-join-external-files/#findComment-110458 Share on other sites More sharing options...
Daniel0 Posted October 18, 2006 Share Posted October 18, 2006 [code]<?php$files = array('file1.txt','file2.txt','file3.txt');foreach($files as $file){ $contents[] = file_get_contents($file);}$fp = fopen('new_file.txt','w+');fwrite($fp,join("\n",$contents));fclose($fp);?>[/code] Link to comment https://forums.phpfreaks.com/topic/24282-join-external-files/#findComment-110510 Share on other sites More sharing options...
Juniorflip Posted October 18, 2006 Author Share Posted October 18, 2006 is there a reason why I am getting this error?[code]Fatal error: Call to undefined function: get_file_contents() in /var/www/html/cs/EmailLauncher/logs/write.php on line 55[/code] Link to comment https://forums.phpfreaks.com/topic/24282-join-external-files/#findComment-110700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.