PHPackages                             mdurrant/php-binary-reader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mdurrant/php-binary-reader

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mdurrant/php-binary-reader
==========================

Lightweight binary reader for PHP

1.0.9(9y ago)62100.3k↑15.2%20[2 PRs](https://github.com/mdurrant/php-binary-reader/pulls)7MITPHPPHP &gt;=5.4

Since Jul 10Pushed 2y ago7 watchersCompare

[ Source](https://github.com/mdurrant/php-binary-reader)[ Packagist](https://packagist.org/packages/mdurrant/php-binary-reader)[ RSS](/packages/mdurrant-php-binary-reader/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (2)Versions (12)Used By (7)

PhpBinaryReader
===============

[](#phpbinaryreader)

[![Build Status](https://camo.githubusercontent.com/385cca99bae961f90147cda59a28a23ab17974f0c4f55fbccbebe413e865b812/68747470733a2f2f7472617669732d63692e6f72672f6d64757272616e742f7068702d62696e6172792d7265616465722e737667)](https://travis-ci.org/mdurrant/php-binary-reader)[![Code Coverage](https://camo.githubusercontent.com/346f01e7ce28f17f8caef31cd37af1164a1226958f2687169382e407660b8501/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d64757272616e742f7068702d62696e6172792d7265616465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mdurrant/php-binary-reader/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/cd8a2da06bf68b0083593b049cf67de8588bd1ff3915eba82dd4415fc75b7b8a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d64757272616e742f7068702d62696e6172792d7265616465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mdurrant/php-binary-reader/?branch=master)

Why?
----

[](#why)

You probably wouldn't be here if you hadn't run into a scenario where you needed to leverage PHP to read a stream of binary data. The honest truth is PHP really stinks at this stuff, but as long as we're going to be using it we may as well do our best to make it as painless as possible.

The purpose of this binary reader is to accept a string of file contents or file resource and provide a set of methods inspired by .NET to traverse it.

Note on Endians
---------------

[](#note-on-endians)

The reader is designed to work on little endian machines, which is going to apply to most scenarios as all x86 and x86-64 machines are little endian. If you have somehow found yourself on a big endian machine, you need to inform the class or you may not be able to properly read signed integers in the file you're parsing.

```
$fileData = file_get_contents('somefile.bin');
$br = new BinaryReader($fileData);
$br->setMachineByteOrder(Endian::ENDIAN_BIG);
...
```

Example Usage
-------------

[](#example-usage)

```
$fileData = file_get_contents('somefile.bin');
$br = new BinaryReader($fileData, Endian::ENDIAN_LITTLE);
// or
$fileResource = fopen('somefile.bin', 'rb');
$br = new BinaryReader($fileResource, Endian::ENDIAN_LITTLE);

$magic = $br->readUInt32();
$offset = $br->readUInt16();
$length = $br->readUInt16();
...
```

Methods
-------

[](#methods)

**\_\_construct($input, $endian)** a string or file resource must be provided to use this class, an endian is optional (string \[big|little\], or use the constants in the Endian class), it will default to little if not provided.

**readUInt8()** returns a single 8 bit byte as an unsigned integer

**readInt8()** returns a single 8 bit byte as a signed integer

**readUInt16()** returns a 16-bit short as an unsigned integer

**readInt16()** returns a 16-bit short as a signed integer

**readUInt32()** returns a 32-bit unsigned integer

**readInt32()** returns a 32-bit signed integer

**readUInt64()** returns a 64-bit unsigned integer

**readInt64()** returns a 64-bit signed integer

**readSingle()** returns a 4-bytes floating-point

**readUBits($length)** returns a variable length of bits (unsigned)

**readBits($length)** returns a variable length of bits (signed)

**readBytes($length)** returns a variable length of bytes

**readString($length)** returns a variable length string

**readAlignedString($length)** aligns the pointer to 0 bits and returns a variable length string

**align()** aligns the pointer back to 0 bits

**isEof()** returns true if the pointer is on the last byte of the file

**getPosition()** returns the current byte position in the file

**setPosition($position)** sets the current byte position

**getCurrentBit()** returns the current bit position in the file

**setCurrentBit($currentBit)** sets the current bit position

Contributing
------------

[](#contributing)

Contributions must follow the PSR2 coding standards and must not degrade 100% coverage.

Acknowledgements
----------------

[](#acknowledgements)

Significant portions of the work is based on Graylin Kim's Python bit/byte reader in `sc2reader`\_

.. \_sc2reader:

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 64.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~97 days

Recently: every ~209 days

Total

10

Last Release

3449d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/92b956749d8c60c399c9af1c00b0cd2ecc99ecaa047208193990ec5c63fa2329?d=identicon)[mdurrant](/maintainers/mdurrant)

---

Top Contributors

[![mdurrant](https://avatars.githubusercontent.com/u/1095505?v=4)](https://github.com/mdurrant "mdurrant (35 commits)")[![jwpage](https://avatars.githubusercontent.com/u/52687?v=4)](https://github.com/jwpage "jwpage (13 commits)")[![nsknewbie](https://avatars.githubusercontent.com/u/3636632?v=4)](https://github.com/nsknewbie "nsknewbie (2 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![klermonte](https://avatars.githubusercontent.com/u/2529340?v=4)](https://github.com/klermonte "klermonte (1 commits)")[![handsomematt](https://avatars.githubusercontent.com/u/1388267?v=4)](https://github.com/handsomematt "handsomematt (1 commits)")[![gingerbeardman](https://avatars.githubusercontent.com/u/49612?v=4)](https://github.com/gingerbeardman "gingerbeardman (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/mdurrant-php-binary-reader/health.svg)

```
[![Health](https://phpackages.com/badges/mdurrant-php-binary-reader/health.svg)](https://phpackages.com/packages/mdurrant-php-binary-reader)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
