note 92344 added to function.unpack
| From: | sicaatwnetcombr at osu1 dot php dot net | Date: | Mon, 20 Jul 2009 19:45:58 +0000 |
| Subject: | note 92344 added to function.unpack | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158519@lists.php.net to get a copy of this message | ||
The script following is a example how to save more than one values on file separating its with
"\r\n" and how to recovering its values.
<?php
// Save two integer values in a binary file
$nomearq = "./teste.bin";
$valor = 123;
$ptrarq = fopen($nomearq, "wb");
$valorBin = pack("L",$valor);
echo "First value ($valor) packed with ";
echo fwrite($ptrarq, $valorBin)." bytes<br>";
echo "Separator \\r\\n with ";
echo fwrite($ptrarq, "\r\n")." bytes<br>";
$valor = 456;
$valorBin = pack("L",$valor);
echo "Second value ($valor) packed with ";
echo fwrite($ptrarq, $valorBin)." bytes<br>";
fclose($ptrarq);
// Recover the saved values
$ptrarq = fopen($nomearq, "rb");
$valorBin = file($nomearq,filesize($nomearq));
echo "<br>The reading values is:<br>";
foreach($valorBin as $valor){
$valor = unpack("L",$valor);
print_r($valor);
echo "<br>";
}
fclose($ptrarq);
?>
Results:
First value (123) packed with 4 bytes
Separator \r\n with 2 bytes
Second value (456) packed with 4 bytes
The reading values is:
Array ( [1] => 123 )
Array ( [1] => 456 )
----
Server IP: 187.16.23.134
Probable Submitter: 200.201.30.184
----
Manual Page -- http://www.php.net/manual/en/function.unpack.php
Edit -- https://master.php.net/note/edit/92344
Del: integrated -- https://master.php.net/note/delete/92344/integrated
Del: useless -- https://master.php.net/note/delete/92344/useless
Del: bad code -- https://master.php.net/note/delete/92344/bad+code
Del: spam -- https://master.php.net/note/delete/92344/spam
Del: non-english -- https://master.php.net/note/delete/92344/non-english
Del: in docs -- https://master.php.net/note/delete/92344/in+docs
Del: other reasons-- https://master.php.net/note/delete/92344
Reject -- https://master.php.net/note/reject/92344
Search -- https://master.php.net/manage/user-notes.php