Torrent File

Packagist GitLab GitHub Bitbucket Gitea

A PHP Class to work with torrent files

Installation

composer require sandfoxme/torrent-file

Usage

Load existing torrent file

<?php

use SandFox\Torrent\TorrentFile;

// from file
$torrent = TorrentFile::load('debian.torrent');
// from string
$torrent = TorrentFile::loadFromString(file_get_contents('debian.torrent'));

Create torrent file for existing directory or file

<?php

use SandFox\Torrent\TorrentFile;

$torrent = TorrentFile::fromPath('/home/user/ISO/Debian');

Save torrent file

<?php

// to file
$torrent->store('debian.torrent');

// to string. for example, for downloading
header('Content-Type: application/x-bittorrent');
header('Content-Disposition: attachment; filename="' . urlencode($torrent->getFileName()) . '"');
echo $torrent->storeToString();

Basic fields manipulation

<?php

// main announce url
$announce = $torrent->getAnnounce();
$torrent->setAnnounce('https://example.com/tracker');

// additional announce urls
$announces = $torrent->getAnnounceList();
// plain ordered list
$torrent->setAnnounceList([
    'https://example.net/tracker',
    'https://example.org/tracker',
]);
// or with tier grouping
$torrent->setAnnounceList([
    ['https://example.com/tracker', 'https://example.net/tracker'],
    'https://example.org/tracker',
]);

// creation date
$created = $torrent->getCreationDate();
$torrent->setCreationDate(time());

// comment
$comment = $torrent->getComment();
$torrent->setComment('This is a very cool torrent');

// created by
$createdBy = $torrent->getCreatedBy();
$torrent->setCreatedBy('Me');

// private marker
$private = $torrent->isPrivate();
$torrent->setPrivate(true);

Possible future features

  • Files model (chunks and offsets for files)
  • Chunks model (files and their offsets, chunk data validation)
  • Info verification for existing files on disk

License

The library is available as open source under the terms of the MIT License.