Solved

file concatenation

Posted on 2004-10-27
488 Views
Last Modified: 2008-02-01
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";
0
Question by:frankenstrat
    14 Comments
     
    LVL 3

    Expert Comment

    by: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.
    0
     
    LVL 1

    Expert Comment

    by:steinmr1
    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
     

    Author Comment

    by:frankenstrat
    I need to remove the first line of each file prior to concatenating them and this is where i am getting lost.
    0
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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
     
    LVL 1

    Expert Comment

    by:steinmr1
    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
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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
     
    LVL 1

    Expert Comment

    by:steinmr1
    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
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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
     

    Author Comment

    by:frankenstrat
    Good point  steinmr1, the files are quite large and I would like to avoid problems. Any suggestions?
    0
     
    LVL 1

    Expert Comment

    by:steinmr1
    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
     

    Author Comment

    by:frankenstrat
    steinmr1 I am a little lost on the pseudocode, if you could expand upon it, it would be greatly appreciated.
    0
     
    LVL 1

    Expert Comment

    by:steinmr1
    Suro, will post it tomorrow (08 GMT)
    0
     
    LVL 49

    Expert Comment

    by: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-
    0
     
    LVL 1

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    860 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now