php – Create torrent file

Question:

I have an Apache server on which the picture is located. You need to create a torrent file that would start the distribution of this picture. I don't need a torrent tracker. I just want to serve a file from the server.

Answer:

You need to create a torrent file that would start the distribution of this picture.

Three options for creating a torrent file.

Let's start by installing the required packages in composer :

  1. Install composer
  2. composer require coldwinds/torrent-rw
  3. Add to file require_once("vendor/autoload.php");
  4. php -f your-file.php
   <?php 
    require __DIR__ . '/vendor/autoload.php';
    // create torrent массив файлов или файл, 
    // URL optional: http://torrent.tracker/annonce
    $torrent = new Torrent( array('you-image.jpg' ), 'http://torrent.tracker/annonce' );
    $torrent->save('test.torrent'); // save to disk
 // На этом в принципе создание торрент файла закончено
 // если поместить его в директорию, которая доступна из вне, то
 // теоритически можно раздавать... 
 // практически придется выбирать трекер из возможных списков
 //----------------------------------------------------------------
 // Если хочется вывести в браузер информацию о созданном файле
  // get torrent infos
  $torrent = new Torrent( './test.torrent' );
echo '<br>private: ', $torrent->is_private() ? 'yes' : 'no', 
     '<br>annonce: ', $torrent->announce(), 
     '<br>name: ', $torrent->name(), 
     '<br>comment: ', $torrent->comment(), 
     '<br>piece_length: ', $torrent->piece_length(), 
     '<br>size: ', $torrent->size( 2 ),
     '<br>hash info: ', $torrent->hash_info(),
     '<br>stats: ';
   var_dump( $torrent->scrape() );
   echo '<br>content: ';
   var_dump( $torrent->content() );
   echo '<br>source: ',
     $torrent;

// get magnet link
$torrent->magnet(); // use $torrent->magnet( false ); to get non html encoded ampersand

torrent-rw dock


Another option for creating a torrent file php: >=5.3.2 :

Team:

composer require christeredvartsen/php-bittorrent

File:

  <?php
    $torrent = PHP\BitTorrent\Torrent::createFromPath('/path/to/files', 'http://tracker/announce.php');

    $torrent->setComment('Some comment')
            ->save('/save/to/path/file.torrent');

php-bittorrent doc


Another php: >=7.1 example:

Team:

composer require sandfoxme/torrent-file

File:

 <?php

 require __DIR__ . '/vendor/autoload.php';

 use SandFoxMe\Torrent\TorrentFile; 

 $torrent = TorrentFile::fromPath('/home/user/your-file.jpg');
 $torrent->store('your-file.torrent');

torrent-file док

Scroll to Top