file concatenation

I have two files that I wish to concatenate with the following code and I am sure I am not approaching this correctly...

$myDirectory = opendir($pathtodata);
      if (!$myDirectory) $mailBody .= "Failed to open $pathtodata for reading\n";
while ($filename = readdir($myDirectory)) {
 if ($filename == "$ListingData.P" && $banfilename=="bancroft.sql"){
   $myfile = fopen("$pathtodata$filename", 'r+');
   $mybanfile = fopen("$pathtodata$banfilename", 'r+');
   if (!$myfile) {
     $mailBody .= "failed to open properties file for editing\n";
     exit;
    } // is read KO
      else if (!$mybanfile) {
     $mailBody .= "failed to open bancroft file for editing\n";
     exit;
    } // is read KO
    else { // let's go

      // prepare for writing
      $newfn="properties.sql";
      $newfile=fopen($newfn,'a+');
      if (!$newfile) $mailBody .= "failed to open properties.sql for editing\n";
      // get first line
      // get subsequent and write them
      $str=file("$pathtodata$filename");
      for ($i=1;$i<count($str);$i++) fputs($newfile,$str[$i]); // effectively ignoring first line (index=0)
      // concatenation will start here.
      // get subsequent and write them
      $banstr=file("$pathtodata$banfilename");
      for ($x=1;$x<count($banstr);$x++) fputs($newfile,$banstr[$x]); // effectively ignoring first line (index=0)
      // close everything
      fclose($myfile);
      fclose($mybanfile);
      fclose($newfile);
      $mailBody .= "Properties dump file created\n";
frankenstratAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KvdnBergCommented:
instead of reading the file line by line in an array using file, you could try fread which returns the entire file as a string. also keep in mind that fputs is just an alias of fwrite so you might as well use fwrite, which is easier to read in combination with fread.
0
steinmr1Commented:
Your code is a bit bloated, but in short, here is what I think you need to do

fopen the file1 (the file you want to add to file2) with read permission.

fopen file2 (the file your want to add to) with append permission
and for each line of file1, write it to file2.

OR you could open a third file and write file1 and file2 to it.

Hope this helps
0
frankenstratAuthor Commented:
I need to remove the first line of each file prior to concatenating them and this is where i am getting lost.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

KvdnBergCommented:
Also, it might be a better idea to put both files in one string and writing to the file only once:

$file1 = fread($myfile);
$file2 = fread($mybanfile);

$complete_file = $file1 . $file2;

fwrite($newfile, $complete_file);

This instead of the part from //get first line until //close everything
0
steinmr1Commented:
to remove the first line of file1 is easy, just skip it when reading. (Set a flag when first line is read)
To remove the first line of file2, you have to create file3

fopen(file1,r)
fopen(file2,r)
fopen(file3,w)

firstlinefile1=false;
foreach line in file1 {
       if(!firstlinefile1) { firstlinefile1=true; continue
        fwrite(getline(file1),file3)
}

firstlinefile2=false;
foreach line in file2 {
       if(!firstlinefile2) { firstlinefile2=true; continue
        fwrite(getline(file2),file3)
}

close all files


hope this helps
0
KvdnBergCommented:
Ok, to remove the first line:
$file1 = fread($myfile);
$file2 = fread($mybanfile);

$secondlinepos1 = strpos($file1,"\n");
$secondlinepos2 = strpos($file2,"\n");

$file1 = substr($file1, $secondlinepos1);
$file2 = substr($file2, $secondlinepos2);

$complete_file = $file1 . $file2;

fwrite($newfile, $complete_file);

Give me a minute to test this...
0
steinmr1Commented:
Not trying to be negative, but reading a file into a string is not a very good idea, unless you _know_ that the files are going to be small.
0
KvdnBergCommented:
Ok, that might be true, but just in case, here's the working code

   $myfile = fopen("$pathtodata$filename", 'r+');
   $mybanfile = fopen("$pathtodata$banfilename", 'r+');

$file1 = fread($myfile, filesize("file1.txt"));
$file2 = fread($mybanfile, filesize("file2.txt"));

$secondlinepos1 = strpos($file1,"\n") + 1;
$secondlinepos2 = strpos($file2,"\n") + 1;

$file1 = substr($file1, $secondlinepos1);
$file2 = substr($file2, $secondlinepos2);

$complete_file = $file1 . "\r\n" . $file2;

     $newfn="properties.sql";
     $newfile=fopen($newfn,'a+');

fwrite($newfile, $complete_file);
0
frankenstratAuthor Commented:
Good point  steinmr1, the files are quite large and I would like to avoid problems. Any suggestions?
0
steinmr1Commented:
If you just use mye pseudocode, and replace it with real functions from php.net, you will have the solution.

I can type it out for you, but then you'd have to wait until tomorrow, and I bet you'll learn a bunch by doing it yourself ;-)
0
frankenstratAuthor Commented:
steinmr1 I am a little lost on the pseudocode, if you could expand upon it, it would be greatly appreciated.
0
steinmr1Commented:
Suro, will post it tomorrow (08 GMT)
0
RoonaanCommented:
When filesize stays below approximately 8mb you could use the next snippet

<?php
  $output_file = 'file1_and_2.txt';
  $input_file_1 = 'file1.txt';
  $input_file_2 = 'file2.txt';
  //open outputstream
  $out = fopen($output_file, 'w');
  //read file 1
  $in = file($input_file1);
  //write file 1
  foreach($in as $line)
   fwrite($out, $line);

  //.. possibly a fwrite($out, "\n"); //at this line

  //read file 2
  $in = file($input_file2);
  //write file 2
  foreach($in as $line)
   fwrite($out, $line);

  //close output stream
  fclose($out);
?>

Regards

-r-
0
steinmr1Commented:
Here you go. Need a lot of error checking ofcourse, but it works....


$filestocon=array("file1","file2");
// Open file for writing
$af=fopen("file3",w);

foreach($filestocon as $filename) {
  $f=fopen($filename,r);
  if(!feof($f)) fgets($f); // Skip first line
   // Add contents to f3
   while (!feof($f)) fwrite($af,fgets($f));
   fclose($f);
}
fclose($af);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.