concat 2 binary files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • herc777@hotmail.com

    concat 2 binary files?

    I want to manipulate binary files.

    this was just a test to concatinate 2 text files but it said
    file_put_conten ts was undefined. the second parameter is suppoed to be
    'mixed data', no idea what that is.

    <?php
    $t1 = file_get_conten ts('test1.txt') ;
    $t2 = file_get_conten ts('test2.txt') ;
    $result = file_put_conten ts('test3.txt', $t1 . $t2);
    ?>

    But this was just a test anyway since file_get_conten ts uses strings,
    can someone just give me the code to concat 2 binary files? I'll be
    manipulating the files before I concatinate them, so putting the data
    into 2 arrays of bytes 1st would be best.

    Herc

  • Janwillem Borleffs

    #2
    Re: concat 2 binary files?

    herc777@hotmail .com wrote:
    [color=blue]
    > I want to manipulate binary files.
    >
    > this was just a test to concatinate 2 text files but it said
    > file_put_conten ts was undefined. the second parameter is suppoed to be
    > 'mixed data', no idea what that is.
    >[/color]

    The file_put_conten ts() function is only supported on PHP v >= 5; the
    indication mixed data means that it can be anything from a scalar to an
    array.
    [color=blue]
    > But this was just a test anyway since file_get_conten ts uses strings,
    > can someone just give me the code to concat 2 binary files? I'll be
    > manipulating the files before I concatinate them, so putting the data
    > into 2 arrays of bytes 1st would be best.
    >[/color]

    An alternative for file_put_conten ts on PHP 4.x would be to apply fopen
    with ``wb'' as the switch and fputs for writing:

    $fp = fopen("somenewf ile", "wb");
    fputs($fp, $data_1 . $data_2);
    fclose($fp);

    Since file_get_conten ts() is binary safe, the binary data will be
    untouched and the merge successful, provided that the binary data
    actually can be merged this way...


    JW


    Comment

    • Steve

      #3
      Re: concat 2 binary files?

      [color=blue]
      > I want to manipulate binary files.[/color]

      Add error handling as appropriate...


      $strFile1 = "blue.bin";
      $strFile2 = "green.bin" ;
      $strFile3 = "bluegreen.bin" ;


      // open for read/binary-safe
      $objFH = fopen( $strFile1, "rb" );
      $strBuffer1 = fread( $objFH, filesize( $strFile1 ) );
      fclose( $objFH );


      // open for read/binary-safe
      $objFH = fopen( $strFile2, "rb" );
      $strBuffer2 = fread( $objFH, filesize( $strFile2 ) );
      fclose( $objFH );


      // ...manipulate buffers here...
      $strBuffer3 = $strBuffer1 . $strBuffer2;


      // open for write/binary-safe
      $objFH = fopen( $strFile3, "wb" );
      fwrite( $objFH, $strBuffer3 );
      fclose( $objFH );


      ---
      Steve

      Comment

      • herc777@hotmail.com

        #4
        Re: concat 2 binary files?

        Thanks, if I join your two answers together I get exactly what I want!

        Herc

        Comment

        Working...