PHPackages                             php-io-extensions/fd - 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. [File &amp; Storage](/categories/file-storage)
4. /
5. php-io-extensions/fd

ActivePhp-ext[File &amp; Storage](/categories/file-storage)

php-io-extensions/fd
====================

PHP-Controllable UNIX FileDescriptor Extension

v0.1.7(4w ago)014MITCPHP &gt;=8.0CI passing

Since Apr 16Pushed 4w agoCompare

[ Source](https://github.com/php-io-extensions/fd)[ Packagist](https://packagist.org/packages/php-io-extensions/fd)[ RSS](/packages/php-io-extensions-fd/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (9)Used By (0)

FD
==

[](#fd)

[![PHP](https://camo.githubusercontent.com/91e2ff786d2fba1edf015025006e0156a071320b3662eaf2c50f39d4bb4b2369/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e332d626c7565)](https://www.php.net)[![Zephir](https://camo.githubusercontent.com/5add3cf7b546a9aa97c8f3d9f5caeda8c9fdf1bd0f90151d5b3e8e25e591ed83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7a65706869722d302e31392532422d6f72616e6765)](https://docs.zephir-lang.com/latest/en/installation)[![Platform](https://camo.githubusercontent.com/ea15c6295f458b2f3c8337baadfadcef9e2ac9a410afae10e366f5196a480475/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6c696e75782532302537432532306d61636f732d6c6967687467726579)](https://www.kernel.org)

PHP-controllable UNIX file descriptor extension built with Zephir.

The FD extension allows PHP to open raw UNIX file descriptors, enabling control beyond simple stream-based I/O. The integer descriptor can be passed into other extensions or used directly for system-level interfaces such as GPIO, I2C, SPI, and UART.

Requirements
------------

[](#requirements)

- PHP 8.x
- A UNIX-like operating system (Linux, macOS)
- Standard C build tools (`gcc`, `make`, PHP development headers)

Installation
------------

[](#installation)

### Via PHP PIE (recommended)

[](#via-php-pie-recommended)

[PHP PIE](https://github.com/php/pie) is the official PHP extension installer. It handles the full build and activation pipeline automatically.

Install PIE if you haven't already:

```
curl -Lo pie.phar https://github.com/php/pie/releases/latest/download/pie.phar
chmod +x pie.phar
sudo mv pie.phar /usr/local/bin/pie
```

Then install the extension:

```
pie install php-io-extensions/fd
```

PIE will build the extension from source, copy the `.so` into PHP's extension directory, and write the ini activation file. No Zephir required — the C source is pre-generated in the package.

### Manual installation

[](#manual-installation)

Clone the repository and run the bundled installer script. PHP dev headers and `gcc` must be present; Zephir is **not** required.

```
git clone https://github.com/php-io-extensions/fd
cd fd
bash install.sh
```

The script builds from the pre-generated C source in `ext/`, installs the `.so`, and enables the extension across all detected SAPIs (CLI, FPM, Apache).

### Verifying installation

[](#verifying-installation)

```
php -m | grep fd
php --ri fd
```

Usage
-----

[](#usage)

All methods are static on the `Fd\FD` class.

### Flags

[](#flags)

`$flags` parameters accept plain integers. They map directly to the POSIX constants from `fcntl.h` — PHP does not define these, so pass the values directly or define your own:

ConstantLinuxmacOS`O_RDONLY``0``0``O_WRONLY``1``1``O_RDWR``2``2``O_NONBLOCK``2048``4`Combine multiple flags with the bitwise OR operator (`|`):

```
// O_WRONLY | O_NONBLOCK on Linux
$fd = Fd\FD::open('/dev/ttyUSB0', 1 | 2048);
```

---

### `FD::open`

[](#fdopen)

Opens a file or device and returns a file descriptor integer.

```
$fd = Fd\FD::open(string $device_path, int $flags): int
```

Returns the file descriptor on success, or a negative value on failure.

```
// Open an I2C device for reading and writing (O_RDWR = 2)
$fd = Fd\FD::open('/dev/i2c-1', 2);

if ($fd < 0) {
    throw new RuntimeException("Failed to open device");
}
```

---

### `FD::close`

[](#fdclose)

Closes an open file descriptor.

```
$result = Fd\FD::close(int $fd): int
```

Returns `0` on success, `-1` on failure.

```
Fd\FD::close($fd);
```

---

### `FD::addFlags`

[](#fdaddflags)

Adds file status flags to an open file descriptor using `fcntl`. Existing flags are preserved; the provided flags are OR'd in.

```
$result = Fd\FD::addFlags(int $fd, int $flags): int
```

Returns `0` on success, `-1` on failure. Useful for setting a descriptor non-blocking after it has already been opened.

```
// Set O_NONBLOCK on an open descriptor (Linux: 2048, macOS: 4)
Fd\FD::addFlags($fd, 2048);

// Combine flags: O_NONBLOCK | O_APPEND on Linux
Fd\FD::addFlags($fd, 2048 | 1024);
```

---

### `FD::read`

[](#fdread)

Reads up to `$bytes_to_read` bytes from a file descriptor and returns them as a string.

```
$data = Fd\FD::read(int $fd, int $bytes_to_read): string
```

Returns the bytes read as a binary string. Returns an empty string on error. A zero-length read (e.g. EOF) also returns an empty string, so callers that need to distinguish the two cases should check the descriptor state beforehand.

```
// Read 2 bytes from a UART device
$bytes = Fd\FD::read($fd, 2);
```

---

### `FD::write`

[](#fdwrite)

Writes bytes to a file descriptor.

```
$written = Fd\FD::write(int $fd, string $data, int $bytes_to_write): int
```

Returns the number of bytes written on success, or `-1` on failure.

```
// Send a command byte over SPI
Fd\FD::write($fd, "\x3A", 1);
```

---

Example: I2C Read/Write
-----------------------

[](#example-i2c-readwrite)

```
// O_RDWR = 2
$fd = Fd\FD::open('/dev/i2c-1', 2);

if ($fd < 0) {
    throw new RuntimeException('Could not open I2C device');
}

// Write a register address
Fd\FD::write($fd, "\x00", 1);

// Read 2 bytes back
$data = Fd\FD::read($fd, 2);

Fd\FD::close($fd);
```

License
-------

[](#license)

Project Saturn Studios, LLC.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance94

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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 ~4 days

Total

8

Last Release

28d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10563160?v=4)[Angel Gonzalez](/maintainers/projectsaturnstudios)[@projectsaturnstudios](https://github.com/projectsaturnstudios)

---

Top Contributors

[![projectsaturnstudios](https://avatars.githubusercontent.com/u/10563160?v=4)](https://github.com/projectsaturnstudios "projectsaturnstudios (9 commits)")

### Embed Badge

![Health badge](/badges/php-io-extensions-fd/health.svg)

```
[![Health](https://phpackages.com/badges/php-io-extensions-fd/health.svg)](https://phpackages.com/packages/php-io-extensions-fd)
```

###  Alternatives

[glicer/sync-sftp

Sync local files with ftp server

212.7k](/packages/glicer-sync-sftp)[venveo/craft-compress

Create smart zip files from Craft assets on the fly

124.7k](/packages/venveo-craft-compress)

PHPackages © 2026

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