Append contents of multiple files to the end of another file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan2k3k4
    Member
    • Oct 2003
    • 1283

    #1

    Append contents of multiple files to the end of another file?

    Does anyone know either in PHP or Perl or CGI (so I can run it via cronjob) how to take the contents of say file1.txt and paste it to the end of file3.txt and then do the same with file2.txt to the end of file3.txt

    This works for DOS:

    append.bat
    Code:
    @echo off
    
    for /f "tokens=* delims=" %%J in (file1.txt) do echo %%J >> file3.txt
    for /f "tokens=* delims=" %%J in (file2.txt) do echo %%J >> file3.txt
    Contents of file1.txt:
    Code:
    hello
    world
    Contents of file2.txt:
    Code:
    world - how are you?
    I am good.
    Expected Result/Expected Contents of file3.txt:
    Code:
    hello
    world
    world - how are you?
    I am good.
    Note: Does not 'overwrite' any part, and must end the line ("\n" in php) at the end of pasting each file. To prevent output like this:

    Code:
    hello
     worldworld - how are you?
     I am good.
    This is what I have written in a .php file:
    [php]<?PHP
    $fp2 = fopen('file2.txt', 'r');
    $fp = "file3.txt";
    if (!($fp = fopen('file3.txt', 'a'))) {
    die('Cannot open log file');
    }
    if (!($fp1 = fopen('file1.txt', 'r'))) {
    die('Cannot open log file');
    }
    if (!($fp2 = fopen('file2.txt', 'r'))) {
    die('Cannot open log file');
    }

    fwrite($fp, file1.txt);
    fwrite($fp, $f2);

    fclose($fp);
    fclose($fp1);
    fclose($fp2);

    ?>[/php]However, this only writes:

    Code:
    file1.txt
    It doesn't write the contents of file1.txt - and seems to ignore the file2 part, I have tweaked it a lot, testing different ways.

    I think for PHP, I'll have to store the contents of file1.txt into an array, then set that array as string data then write the string data into file3.txt then do the same for file2.txt - but I don't know how to do that, and there may be a simpler way.
  • Xodus
    Member
    • Apr 2006
    • 846

    #2
    Re: Append contents of multiple files to the end of another file?

    I'll get some code up.

    Comment

    • Xodus
      Member
      • Apr 2006
      • 846

      #3
      Re: Append contents of multiple files to the end of another file?

      Here you go:

      [php]
      <?php
      $file1 = "./1.txt";
      $file2 = "./2.txt";
      $file3 = "./file3.txt";

      $f1Length = filesize($file1);
      $f2Length = filesize($file2);


      if($file3Hand = fopen($file3,"a"))
      {
      if($hand = fopen($file1,"r"))
      {
      if(fwrite($file3Hand,fread($hand,$f1Length)))
      {
      if($hand2 = fopen($file2,"r"))
      {
      if(fwrite($file3Hand,"\r\n".fread($hand2,$f2Length )))
      {
      include("./file3.txt");
      }
      }
      }
      }
      }
      ?>
      [/php]

      Comment

      • Dan2k3k4
        Member
        • Oct 2003
        • 1283

        #4
        Re: Append contents of multiple files to the end of another file?

        Sweet, I'll try it out later, but umm

        if($file3Hand = fopen($file3,"a"))

        Where is $file3Hand ? Or can you add 'Hand' to the end of variables? Sorta like arrays when you can do someArray.size etc.

        Comment

        • Xodus
          Member
          • Apr 2006
          • 846

          #5
          Re: Append contents of multiple files to the end of another file?

          $file3Hand is a variable I create within the if() statement, so if it is able to successfully create and assign the variable, it will go into the if brackets. Can't explain it really right now as I have to leave. But it has to do with confirmation of it being made and scope.

          Comment

          • Dan2k3k4
            Member
            • Oct 2003
            • 1283

            #6
            Re: Append contents of multiple files to the end of another file?

            Ahh roger that, didn't know you could do that in php... that's pretty sweet...

            Comment

            • Dan2k3k4
              Member
              • Oct 2003
              • 1283

              #7
              Re: Append contents of multiple files to the end of another file?

              Tested it, and it works - but had to edit the first append so I can use it again and again....

              [php] $file1 = "./1.txt";
              $file2 = "./2.txt";
              $file3 = "./file3.txt";

              $f1Length = filesize($file1);
              $f2Length = filesize($file2);


              if($file3Hand = fopen($file3,"a"))
              {
              if($hand = fopen($file1,"r"))
              {
              if(fwrite($file3Hand,"\r\n".fread($hand,$f1Length) ))
              {
              if($hand2 = fopen($file2,"r"))
              {
              if(fwrite($file3Hand,"\r\n".fread($hand2,$f2Length )))
              {
              include("./file3.txt");
              }
              }
              }
              }
              }
              ?> [/php]Added "\r\n" so it adds a new line before appending the contents of file 1 to the end of file 3...

              Cheers for that!

              Comment

              • Xodus
                Member
                • Apr 2006
                • 846

                #8
                Re: Append contents of multiple files to the end of another file?

                Ah, okay, forgot about about that part

                Comment

                • Dan2k3k4
                  Member
                  • Oct 2003
                  • 1283

                  #9
                  Re: Append contents of multiple files to the end of another file?

                  Hmm anyone know if you can Append via FTP using PHP script? Basically I have the Unix server with PHP and have a cron job to connect to an FTP server (windows) to rename a file, but I realised that if I run it a few times it'll just either give an error because the new file it wants to rename to already exists, or it'll overwrite that file... not sure... but I want it to append to the end of that file...

                  Example:

                  Week1
                  File1.log is on the FTP server
                  File1.log gets renamed to File2.log

                  Week2
                  File1.log is on FTP
                  File1.log appends to end of existing File2.log

                  etc.

                  Therefore if I had "Hello World" inside File1.log

                  File2 after a few weeks would have "Hello World" a few times (seperated by new lines)

                  The only things I can find is from 99 when people posted about bugs for PHP not having append working for FTP

                  EDIT: I have a simple batch file that appends files...

                  Code:
                  @echo off
                  
                  echo Starting.
                  for /f "tokens=* delims=" %%J in (file1.txt) do echo %%J >> file3.txt
                  echo Done 1st File.
                  for /f "tokens=* delims=" %%J in (file2.txt) do echo %%J >> file3.txt
                  echo Done 2nd File.
                  Now I'll just need to find out if it's possible to 'execute' batch files via a php script remotely...

                  EDIT2: Don't worry about all this, I'm doing it a different way... just gonna store each file on the server and download it from there, instead of how I used to do it (by downloading it from each game server) - this way I'll be able to store each log too, wooh...

                  Comment

                  Working...