![]() |
|
![]() | ||
|
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 |
![]() | ||
|
There is no answer at this time. |
![]() | ||
|
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"; } } ?> |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |