Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


extracting information from .torrent files

Posted on 2004-04-10
Medium Priority
Last Modified: 2012-05-04
I would like to use PHP to extract information from .torrent files, which are created according the BitTorrent protocol. Upon opening a .torrent file with a text editor, I obse4rved that the address of the relevent tracker is the first thing to appear in such files. The rest of the file's contents were in an encoding that could not be understood by viewing the file in a text editor. However, I'm pretty sure that .torrent files contain a list of the shared folder and file names, in addition to the hash code of each file. Is there a PHP library that could assist me in extracting these folder and file names? If not, how can a PHP script get access to the file names that are stored within a .torrent file?  Thank you.
Question by:bobwood2000
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 12

Expert Comment

ID: 10799224
There is no such library in php.
LVL 12

Expert Comment

ID: 10799236
First of all why do you want to use php to do something like this.
Such things is supposed to be done in non-scripting languages like C/C++ java...

I dont want to discourage you, but think of your language choice..

You have to read the file in binary mode using fread() and manipulate data accordingly.


Author Comment

ID: 10799312
I was hoping to use PHP to extract this information from a .torrent file so that I could keep my entire web site in a single language. Since there is no such library (thank you, venkateshwarr), I would like to know what language I should use to extract this data from the torrent file. I suppose that I could have PHP call the program that is written in this other language. I would now like to know if there exists a library (in any language) that makes it easy to understand the data in a torrent file?

Thank you.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 10799402
Did you try for any open source codes which you can use...

Accepted Solution

TheClickMaster earned 2000 total points
ID: 10799791
I wrote this. I hope I will get the points since it took me over 2 hours =)

This will parse a torrent file and display the array. You may use this to
create whatever you want to do with it.

I hope you can use it and I havent wasted 2 hours of my life although it was
very interesting to program. It has a recursive function that explores the torrent file.

To see it working have a look at: http://theclickmaster.com/extract.php

There is still a bug with the "pieces" since they should all have a lenght of 20 but it definitely doesnt work right. Some kind of binary encoding ... If anyone knows what it is contact me ;)


// Torrent Parser by mYsTic aka TheClickMaster
// 11-april-2004
// Contact: wi_dark@hotmail.com
// For more information on the bittorrent
// specifications go to:
// http://bitconjurer.org/BitTorrent/protocol.html

// open a file
// >>>>>> customize this
$file = fopen("./ap.torrent","r");

// read it
$contents = fread($file, filesize("./x.torrent"));

// close file

// decode the data. torrent stuff is utf8 encoded
$contents = utf8_decode($contents);

// start the whole process.

// splits the data from a pieces element into individual
// strings of length 20, each of which is the SHA1 hash
// of the piece at the corresponding index.
function get_pieces($content,&$Data)
      //$content = bin2hex($content);
      // get the lenght of the data
      $lenght = strlen($content);
      // calculate number of pieces
      $nb_pieces = $lenght / 20;
      // go throught the string and separate it into pieces
      for ($Cpt = 0; $Cpt < $nb_pieces; $Cpt ++)
            // get a substring
            $Data["Piece " + strval($Cpt)] = substr($content,$Cpt * 20,20);

// explore the data from a torrent file
function explore($content,&$Data,$parent_name)
      // current position
      $Cpt = 0;
      //sub array counter
      $sub_arr = 0;
      // buffer, containts the lenght for the next text field
      $buffer = "";
      // name of the current dictionnary element
      $iname = "";
      // while we are not at the end of the data
      // and we are not at the end of the current element
      while($Cpt < strlen($content) && $content[$Cpt] != "e")
            // if we have a dictionnary, list or integer
            if($content[$Cpt] == "d" || $content[$Cpt] == "l" || $content[$Cpt] == "i")
                  // If this item has no name
                  if($iname == "")
                        // create a generic name based on the parents name and
                        // a unique number
                        $iname = $parent_name."-".$sub_arr;
                        // increase the number
                        $sub_arr ++;
                  // call the function recursively to parse this item
                  // the function will also return a number that tells
                  // us how far we have to move in the data and we
                  // increase $Cpt in consequence
                  $Cpt += explore(substr($content,$Cpt+1),&$Data[$iname],$iname);

                  // clear our item name
                  $iname = "";
                  // if we have the begining of a string
                  if ($content[$Cpt] == ":")
                        // get the lenght of the string from the buffer
                        $lenght = intval($buffer);
                        // if we do not have an element name saved
                        if ($iname == "")
                              // means this string is the name of an element
                              // set the element name to the substring based on the lenght
                              $iname = substr($content,$Cpt+1,$lenght);
                              // clear our lenght buffer
                              $buffer = "";
                              // we alrady have a name so this must be a string element
                              // if this is the "pieces" element
                              if ($iname == "pieces")
                                    // decompose the data
                                    $Dat[$iname] = get_pieces(substr($content,$Cpt+1,$lenght),&$Data[$iname]);
                                    // get the string and save it with the current element name
                                    $Data[$iname] = substr($content,$Cpt+1,$lenght);
                              // clear the buffer and the element name
                              $iname = "";
                              $buffer = "";
                        // move further in the data
                        // we skip the equivalent of the lenght of this string
                        $Cpt += $lenght;
                        // it is a number that will tell us the lenght of the next string
                        $buffer .= $content[$Cpt];      
            // move 1 character
            $Cpt ++;
      // if we still have data in $buffer
      // that means this was an Integer element
      // so we save the buffer's value in the current array element
      if ($buffer != "") $Data = intval($buffer);

      // return the lenght of what we have explored
      return $Cpt + 1;

// dump the array using <pre> to make it pretty
echo "<pre>";
echo "</pre>";

// thats all =)

Author Comment

ID: 10800157
Wow! Thank you very much.

Expert Comment

ID: 10800978
No problem I enjoyed doing it

Power of PHP ;)


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question