PHPackages                             jstewmc/encode-file - 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. jstewmc/encode-file

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

jstewmc/encode-file
===================

Encode a file.

v0.2.0(9y ago)145MITPHPPHP ^7.0

Since Aug 27Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jstewmc/encode-file)[ Packagist](https://packagist.org/packages/jstewmc/encode-file)[ RSS](/packages/jstewmc-encode-file/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

encode-file
===========

[](#encode-file)

Encode a file.

```
use Jstewmc\EncodeFile\Encode;

// set the filename
$filename = '/path/to/foo.txt';

// create an ASCII encoded string
$contents = mb_convert_encoding('foo', 'ASCII');

// put the contents into the file
file_put_contents($filename, $contents);

// is the file UTF-32 encoded?
mb_check_encoding(file_get_contents($filename), 'UTF-32');  // returns false

// create the service
// keep in mind, you'll need to implement the Read and Write interfaces
//
$service = new Encode(new Read(), new Write());

// convert the file to UTF-32
$service($filename, 'UTF-32');

// is the file UTF-32 encoded?
mb_check_encoding(file_get_contents($filename), 'UTF-32');  // returns true
```

PHP Extensions
--------------

[](#php-extensions)

This library requires PHP's non-default [`mbstring`](http://php.net/manual/en/book.mbstring.php) extension. If the service is instantiated without the `mbstring` extension loaded, a `BadMethodCallException` will be thrown.

Dependencies
------------

[](#dependencies)

This library depends on (and provides) two interfaces for a *read-file service* and a *write-file service*. The former must implement a `__invoke(string $filename): string` method, and the latter must implement a `__invoke(string $filename, string $contents): int` method.

The *read-file* and *write-file* interfaces MAY be implemented by the [jstewmc/read-file](https://github.com/jstewmc/read-file) and [jstewmc/write-file](https://github.com/jstewmc/write-file) libraries, respectively. For example, in your application, extend `Jstewmc\ReadFile\Read` and implement `Jstewmc\EncodeFile\Read`:

```
namespace My\App;

use Jstewmc\EncodeFile\Read as ReadInterface;
use Jstewmc\ReadFile\Read as ReadParent;

class Read extends ReadParent implements ReadInterface
{
    // nothing yet
}
```

From encoding
-------------

[](#from-encoding)

Keep in mind, it's difficult to detect a string's character encoding. Even PHP's [`mb_detect_encoding()`](http://php.net/manual/en/function.mb-detect-encoding.php) function is not perfect. For example, `mb_detect_encoding()` will almost never detect *Windows-1252* encoding, even if the string actually is *Windows-1252* encoded (see [Bug #64667](https://bugs.php.net/bug.php?id=64667) for details).

To prevent erroneously detecting the file's *from* encoding, you MAY include it as the service's third argument. If the "from" encoding is not given, the library will attempt to detect it.

```
use Jstewmc\EncodeFile\Encode;

$service = new Encode(new Read(), new Write());

// encode file as UTF-8 from Windows-1252
$service('/path/to/file.txt', 'UTF-8', 'Windows-1252');
```

Author
------

[](#author)

[Jack Clayton](mailto:clayjs0@gmail.com)

License
-------

[](#license)

[MIT](https://github.com/jstewmc/encode-file/blob/master/LICENSE)

Version
-------

[](#version)

### 0.3.0, September 6, 2016

[](#030-september-6-2016)

- Add `Read` and `Write` interfaces.
- Delete dependency on [jstewmc/read-file](https://github.com/jstewmc/read-file) and [jstewmc/write-file](https://github.com/jstewmc/write-file).

### 0.2.0, August 31, 2016

[](#020-august-31-2016)

- Rename repository to `encode-file`.
- Refactor library to use [jstewmc/read-file](https://github.com/jstewmc/read-file) and [jstewmc/write-file](https://github.com/jstewmc/write-file)

### 0.1.0, August 27, 2016

[](#010-august-27-2016)

- Initial release

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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

2

Last Release

3590d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/50fecae0a7fd2119681bc133e496e7166b01a59f850a3c909e100bd427c6b28b?d=identicon)[Jstewmc](/maintainers/Jstewmc)

---

Top Contributors

[![jstewmc](https://avatars.githubusercontent.com/u/1192893?v=4)](https://github.com/jstewmc "jstewmc (10 commits)")

---

Tags

phpfileencodejstewmc

### Embed Badge

![Health badge](/badges/jstewmc-encode-file/health.svg)

```
[![Health](https://phpackages.com/badges/jstewmc-encode-file/health.svg)](https://phpackages.com/packages/jstewmc-encode-file)
```

###  Alternatives

[blueimp/jquery-file-upload

File Upload widget for jQuery.

291.5M19](/packages/blueimp-jquery-file-upload)[davaxi/vcalendar

PHP Class to generate VCalendar (ics) file

1182.9k](/packages/davaxi-vcalendar)

PHPackages © 2026

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