extracting information from .torrent files

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.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

TheClickMasterConnect With a Mentor Commented:
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 =)
There is no such library in php.
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.

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

bobwood2000Author Commented:
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.
Did you try for any open source codes which you can use...
bobwood2000Author Commented:
Wow! Thank you very much.
No problem I enjoyed doing it

Power of PHP ;)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.