Loading, Saving and Creating#
Load an existing torrent#
You can load a torrent from file, from string, or from stream.
<?php
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#
You can save your torrent to file, to string, or to stream.
<?php
// to file
$torrent->store('debian.torrent');
// to string
$torrentString = $torrent->storeToString();
// to stream
$torrent->storeToStream($stream);
// to new php://temp stream
$phpTemp = $torrent->storeToStream();
Create a torrent for existing directory or file#
Added in version 5.1: forceMultifile is true by default
Added in version 5.3: createdBy, creationDate
The library can create a torrent file from scratch for a file or a directory.
<?php
use Arokettu\Torrent\TorrentFile;
$torrent = TorrentFile::fromPath(
'/home/user/ISO/Debian',
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:
versionBitTorrent metadata file version.
MetaVersion::V1as described in BEP-3 spec.MetaVersion::V2as 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](wasMetaVersion::V1in 2.x)pieceLengthThe 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)pieceAlignAlign 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 filesfalse: Do not alignint $bytes: Align files larger than$bytesin length
Default:
falsedetectExecThe library detects executable attribute and sets it on files. Default:
truedetectSymlinksThe library detects symlinks and creates symlink torrent objects. Only symlinks leading to files in the torrent data directory are detected. Default:
falseforceMultifileV1 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:
truecreatedByOverride
created byfield for the created torrent. Passnullto unset. Default: the library name and url.creationDateOverride
creation datefield for the created torrent. Accepts instances ofDateTimeInterfaceandClockInterfaceand integer timestamps. Passnullto unset. Default: the current timestamp.
Note
Defaults may change in minor versions. If you care about their specific values, set them explicitly.
Warning
Parameter order is not guaranteed for options. Please use named parameters.