使用BDecode从torrent文件中获取元数据

时间:2013-12-30 06:28:12

标签: php

我正在尝试获取洪流的名称和大小。我正在使用BDecode:Parse and extract metadata from BitTorrent files类从torrent文件中获取信息。

Var_dump结果

array(1) {
  ["info"]=>
  array(4) {
    ["files"]=>
    array(2) {
      [0]=>
      array(2) {
        ["length"]=>
        int(735053240)
        ["path"]=>
        array(1) {
          [0]=>
          string(54) "BULLET RAJA [2013] -HDRiP XViD MP3  [D3Si MaNiACs].avi"
        }
      }
      [1]=>
      array(2) {
        ["length"]=>
        int(3226657)
        ["path"]=>
        array(1) {
          [0]=>
          string(10) "sample.mkv"
        }
      }
    }
    ["name"]=>
    string(50) "BULLET RAJA [2013] -HDRiP XViD MP3  [D3Si MaNiACs]"
    ["piece length"]=>
    int(1048576)
    ["pieces"]=>
    string(14100) "xxxxxxxxxxxxxxx"

到目前为止我做了什么

<?php
    require_once 'class.bdecode.php';

    $torrent = new BDECODE('myfile.torrent');
    $results = $torrent->result;

    print '<pre>';
    var_dump($torrent->result);
    print '</pre>';

    echo '<hr/>';

    foreach($results as $value){
        echo $value['name'];
    }

    foreach($results as $length){
        foreach($length as $value){
            echo $value["length"];
        }
    }

?>

我的问题,我能够获得洪流的名称,但无法获得洪流的文件大小。我收到了这个错误。

BULLET RAJA [2013] -HDRiP XViD MP3 [D3Si MaNiACs]
Notice: Undefined index: length in D:xxxxxxxx\example.php on line 27

    Warning: Illegal string offset 'length' in D:xxxxxx\example.php on line 27
    B
    Warning: Illegal string offset 'length' in D:xxxxxx\example.php on line 27
    �

1 个答案:

答案 0 :(得分:1)

torrent的名字:

$torrent->result['info']['name'];

文件列表,长度为:

if(isset($torrent->result['info']['files'])) { // for directory torrent
    foreach($torrent->result['info']['files'] as $file) {
        echo implode('\\',$file['path']).' - '.$file['length'];
    }
} else {  // for single file torrent
    echo $torrent->result['info']['name'].' - '.$torrent->result['info']['length'];
}