Google Answers Logo
View Question
 
Q: Script required for writing pure binary in PHP ( No Answer,   4 Comments )
Question  
Subject: Script required for writing pure binary in PHP
Category: Computers
Asked by: document123-ga
List Price: $10.00
Posted: 19 Jun 2004 02:26 PDT
Expires: 22 Jun 2004 04:45 PDT
Question ID: 363261
I'm an experienced programmer but relative beginner to PHP and am
struggling to write binary data to a file in PHP - I've looked at the
Coggeshall guide but I'm still struggling.

Could someone please write a PHP function called WriteToBinary here
where for instance I would pass it the hexadecimal values only in s
string as follows

$output_file == 'C:\temp.txt';
$input_string == '5A0010D3A8C6000000D9C5E2D6E4D9C3C5';
WriteToBinary($input_string,$output_file);

Where each set of two characters is one byte in the binary file

This function would write this record to a binary file (you can assume
it will be CRLF delimited records but preferably I'd like to be able
to amend this so assign this delimiter as an amendable constant)

Equally if this isn't possible in PHP then let me know.

The answer would need to be heavily commented and self documenting so
that an idiot like myself can understand it.

Thanks very much in advance.

Clarification of Question by document123-ga on 20 Jun 2004 14:49 PDT
Thank you very very much for the comments - most helpful - I'll look into these.

However I may be wasn't clear that my answer needs to be a class /
function which I can call with my string.

I'll tip heavily for a good answer - I prefer to do it this way in
case I get an answer which isn't really suitable.

Clarification of Question by document123-ga on 22 Jun 2004 04:41 PDT
crythias-ga,

Thank you very much for your comments - I knew how to write files
before, I just wanted to write the binary.
I had got this far already with the comments about the pack() command
which I didn't know already done - but it's not quite there and sicne
you're not a GA researcher you can't place this as an answer
apparently.

It would need to create the file first time through - so the
is_writable check is no good - and I didn't want the echo's - I just
wanted to create the file write the first record and then append as I
went along in the script.

I can now work it out now though - so as requested I'll cancel this question.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Script required for writing pure binary in PHP
From: crythias-ga on 19 Jun 2004 13:20 PDT
 
Check out this: http://us4.php.net/manual/en/function.fwrite.php
There are nice comments how to send binaries to files or MySQL.
Subject: Re: Script required for writing pure binary in PHP
From: crythias-ga on 21 Jun 2004 15:07 PDT
 
ok, I guess this is still needing to be answered, or else you would
have cancelled this, or you're ignoring it...
From this page http://www.php.net/manual/en/language.functions.php we
copy the example, change the word foo to the word WriteToBinary.
Reduce the argument list to two arguments, clear the echo and return
lines and paste the example from the above link inside the function,
trimming the second example of <?php and ?>.
Thus, the combined function and binary write is:

<?php
function WriteToBinary($arg_1, $arg_2)
{
$filename = $arg_2;
$somecontent = $arg1;

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
  
   echo "Success, wrote ($somecontent) to file ($filename)";
  
   fclose($handle);
                  
} else {
   echo "The file $filename is not writable";
}
}
?> 

This very much will be the answer you requested, exactly. I am not a
Google Answers Researcher. If you feel this has answered your
question, please cancel/expire it. If you need clarification, go ahead
and ask, but the documentation for either "function" or "write" has
been very nicely detailed at the given links. The content is copied
under the terms of the Copyright:  Copyright © 1997 - 2004 by the PHP
Documentation Group. This material may be distributed only subject to
the terms and conditions set forth in the Open Publication License,
v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
http://www.php.net/manual/en/copyright.php
Subject: Re: Script required for writing pure binary in PHP
From: crythias-ga on 21 Jun 2004 15:09 PDT
 
I never get this right. 
$somecontent = $arg1; should be
$somecontent = $arg_1;

Sorry about that.
Subject: Re: Script required for writing pure binary in PHP
From: crythias-ga on 21 Jun 2004 19:25 PDT
 
OK, Shall we try one last time?
Using examples in comments at 
http://www.php.net/manual/en/function.bin2hex.php and
http://us2.php.net/manual/en/function.pack.php and above code:
<?php
function WriteToBinary($arg_1, $arg_2)
{
$filename = $arg_2;
$data = $arg_1;
// how long is the string?
$len = strlen($data);
//pack it into a binary string
$somedata = pack("H" . $len, $data); 
// note: some people say pack("H*", $data); is enough. IMHO it's equivalent.
// a replacement for the $somedata= pack... follows 
// COMMENT: CLEAR $somedata
//$somedata="";
// COMMENT: cycle through the data, 2 chars at a time
//for( $i = 0; $i < strlen( $data ); $i += 2 )
// COMMENT: take the character, and the next, and make it a binary character.
//eval( '$somedata .= "\x' . substr( $data, $i, 2 ) . '";' );

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
  
   echo "Success, wrote ($somecontent) to file ($filename)";
  
   fclose($handle);
                  
} else {
   echo "The file $filename is not writable";
}
}
?>

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy