Link to home
Start Free TrialLog in
Avatar of frankenstrat
frankenstrat

asked on

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";
Avatar of KvdnBerg
KvdnBerg

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.
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
Avatar of frankenstrat

ASKER

I need to remove the first line of each file prior to concatenating them and this is where i am getting lost.
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
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
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...
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.
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);
Good point  steinmr1, the files are quite large and I would like to avoid problems. Any suggestions?
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 ;-)
steinmr1 I am a little lost on the pseudocode, if you could expand upon it, it would be greatly appreciated.
Suro, will post it tomorrow (08 GMT)
Avatar of Roonaan
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-
ASKER CERTIFIED SOLUTION
Avatar of steinmr1
steinmr1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial