Torrent File

Packagist GitLab GitHub Bitbucket Gitea

A PHP Class to work with torrent files


composer require sandfoxme/torrent-file


Load existing torrent file


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


use SandFox\Torrent\TorrentFile;

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

Save torrent file


// to file

// 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


// main announce url
$announce = $torrent->getAnnounce();

// additional announce urls
$announces = $torrent->getAnnounceList();
// plain ordered list
// or with tier grouping
    ['', ''],

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

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

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

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

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


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