Jump to content


Photo

Join External files


  • Please log in to reply
5 replies to this topic

#1 Juniorflip

Juniorflip
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 October 2006 - 12:22 AM

Hopefully this is a simple function to do but I am looking to join 3 external file and output to a single file

example

header.txt = hello
body.txt = welcome
footer.txt = goodbye

I need a php function that will open all three text file and print the value to a new text file

example

greeting.txt = hello
                    welcome
                    goodbye

Is this possible to do?

Thanks in advance

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 18 October 2006 - 03:13 AM

This should work

$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);

It's better if you check every function call for errors, like

if ($header === false) die("Couldn't read header.txt\n");


#3 Juniorflip

Juniorflip
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 October 2006 - 03:34 AM

So if I wanted to make a function it would go like this

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();


#4 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 18 October 2006 - 03:46 AM

Yes.. and for every other function too.

It may seem like a lot of work, but it saves a lot of debugging work later.

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();

It's not so essential to check fwrite() and fclose(), since they very rarely fail.

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 07:31 AM

<?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);
?>


#6 Juniorflip

Juniorflip
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 October 2006 - 02:32 PM

is there a reason why I am getting this error?

Fatal error: Call to undefined function: get_file_contents() in /var/www/html/cs/EmailLauncher/logs/write.php on line 55





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users