Loading, Saving and Creating

Load an existing torrent

Added in version 1.2: loadFromString()

Added in version 2.1: loadFromStream()

You can load a torrent from file, from string, or from stream.


use Arokettu\Torrent\TorrentFile;

// from file
$torrent = TorrentFile::load('debian.torrent');
// from string
$torrent = TorrentFile::loadFromString(file_get_contents('debian.torrent'));
// from stream
$torrent = TorrentFile::loadFromStream(fopen('debian.torrent', 'r'));

Save torrent

Added in version 1.2: storeToString()

Added in version 2.1: storeToStream()

You can save your torrent to file, to string, or to stream.


// to file
// to string
$torrentString = $torrent->storeToString();
// to stream
// to new php://temp stream
$phpTemp = $torrent->storeToStream();

Create a torrent for existing directory or file

Added in version 1.1: $options

Added in version 2.0: $eventDispatcher

Added in version 2.2: pieceAlign, detectExec, detectSymlinks

Changed in version 2.2: sortFiles, md5sum became noop

Added in version 2.3/3.1: version

Added in version 2.5/3.3/4.1: forceMultifile

Changed in version 4.1: MetaVersion::HybridV1V2 is now an array [MetaVersion::V1, MetaVersion::V2]

Added in version 5.0.1: $clock

Added in version 5.1.0: forceMultifile is true by default

The library can create a torrent file from scratch for a file or a directory.


use Arokettu\Torrent\TorrentFile;

$torrent = TorrentFile::fromPath(
    pieceLength: 512 * 1024,

// pass an instance of PSR-14 event dispatcher to receive progress events:
$torrent = TorrentFile::fromPath('/home/user/ISO/Debian', $eventDispatcher);
// dispatcher will receive instances of \Arokettu\Torrent\FileSystem\FileDataProgressEvent
//    only in 2.0 and later

Available options:


BitTorrent metadata file version.

  • MetaVersion::V1 as described in BEP-3 spec.

  • MetaVersion::V2 as described in BEP-52 spec.

  • A list [MetaVersion::V1, MetaVersion::V2] for a hybrid torrent both V1 and V2 metadata.

Default: [MetaVersion::V1, MetaVersion::V2] (was MetaVersion::V1 in 2.x)


The number of bytes that each logical piece in the peer protocol refers to. Must be a power of 2 and at least 16 KiB. Default: 524_288 (512 KiB)


Align files to piece boundaries by inserting pad files. The option is ignored for V2 and V1+V2 torrent files because files in V2 are always aligned.

  • true: Align all files

  • false: Do not align

  • int $bytes: Align files larger than $bytes in length

Default: false


The library detects executable attribute and sets it on files. Default: true


The library detects symlinks and creates symlink torrent objects. Only symlinks leading to files in the torrent data directory are detected. Default: false


V1 torrents are created in ‘directory’ mode even when created for a single file. This mode fixes some possible incompatibilities between V1 and V2 data in hybrid torrents. Always enabled in hybrid torrents, meaningless for pure V2. Default: true


A parameter to inject a clock component, mostly for debug purposes. To set the creation timestamp normally, use setCreationDate($timestamp) on the created torrent object. Default: a clock that returns a current timestamp.


Defaults may change in minor versions. If you care about their specific values, set them explicitly.


Parameter order is not guaranteed for options. Please use named parameters.