2011-07-06 42 views
2

我正在努力從我的媒體服務器上獲取一些id3標籤。我有我發現挖掘ID3標籤了MP3的

<?php 
// From here: http://www.autistici.org/ermes/index.php?pag=1&post=15 
// and fixed here: http://www.barattalo.it 
// ------------------------------ 
// example: 
// print_r(tagReader ("myfile.mp3")); 
// ------------------------------ 
function tagReader($file){ 
    $id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT"); 
    $id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT"); 
    $fsize = filesize($file); 
    $fd = fopen($file,"r"); 
    $tag = fread($fd,$fsize); 
    $tmp = ""; 
    fclose($fd); 
    if (substr($tag,0,3) == "ID3") { 
     $result['FileName'] = $file; 
     $result['TAG'] = substr($tag,0,3); 
     $result['Version'] = hexdec(bin2hex(substr($tag,3,1))).".".hexdec(bin2hex(substr($tag,4,1))); 
    } 
    if($result['Version'] == "4.0" || $result['Version'] == "3.0"){ 
     for ($i=0;$i<count($id3v23);$i++){ 
      if (strpos($tag,$id3v23[$i].chr(0))!= FALSE){ 
       $pos = strpos($tag, $id3v23[$i].chr(0)); 
       $len = hexdec(bin2hex(substr($tag,($pos+5),3))); 
       $data = substr($tag, $pos, 9+$len); 
       for ($a=0;$a<strlen($data);$a++){ 
        $char = substr($data,$a,1); 
        if($char >= " " && $char <= "~") $tmp.=$char; 
       } 
       if(substr($tmp,0,4) == "TIT2") $result['Title'] = substr($tmp,4); 
       if(substr($tmp,0,4) == "TALB") $result['Album'] = substr($tmp,4); 
       if(substr($tmp,0,4) == "TPE1") $result['Author'] = substr($tmp,4); 
       if(substr($tmp,0,4) == "TRCK") $result['Track'] = substr($tmp,4); 

       if(substr($tmp,0,4) == "TDRC") $result['Year'] = substr($tmp,4); 
       if(substr($tmp,0,4) == "TLEN") $result['Lenght'] = substr($tmp,4); 
       if(substr($tmp,0,4) == "USLT") $result['Lyric'] = substr($tmp,7); 
       $tmp = ""; 
      } 
     } 
    } 
    if($result['Version'] == "2.0"){ 
     for ($i=0;$i<count($id3v22);$i++){ 
      if (strpos($tag,$id3v22[$i].chr(0))!= FALSE){ 
       $pos = strpos($tag, $id3v22[$i].chr(0)); 
       $len = hexdec(bin2hex(substr($tag,($pos+3),3))); 
       $data = substr($tag, $pos, 6+$len); 
       for ($a=0;$a<strlen($data);$a++){ 
        $char = substr($data,$a,1); 
        if($char >= " " && $char <= "~") $tmp.=$char; 
       } 
       if(substr($tmp,0,3) == "TT2") $result['Title'] = substr($tmp,3); 
       if(substr($tmp,0,3) == "TAL") $result['Album'] = substr($tmp,3); 
       if(substr($tmp,0,3) == "TP1") $result['Author'] = substr($tmp,3); 
       if(substr($tmp,0,3) == "TRK") $result['Track'] = substr($tmp,3); 
       if(substr($tmp,0,3) == "TYE") $result['Year'] = substr($tmp,3); 
       if(substr($tmp,0,3) == "TLE") $result['Lenght'] = substr($tmp,3); 
       if(substr($tmp,0,3) == "ULT") $result['Lyric'] = substr($tmp,6); 
       $tmp = ""; 
      } 
     } 
    } 
    return $result; 
} 

和subsequest呼叫

print_r(tagReader("/mnt/data/downloads/music/Arctic Monkeys/Who The F Are Arctic Monkeys_ - EP/05 Who The F Are Arctic Monkeys_.mp3")); 

的我複製該文件到一個簡單的路徑(甚至有空格)的方法,該方法可以正常工作。它似乎並沒有處理長文件名。我試圖用\逃脫空間,但那不起作用。

+2

你得到的錯誤是什麼?你確定這個文件是可讀的嗎? –

+1

請啓用錯誤報告並顯示:'error_reporting(-1); ini_set('display_errors',1);'然後再次處理你遇到錯誤的情況。我假設你會收到一條錯誤消息,然後你可以添加到你的問題。 – hakre

+1

您的Web服務器的用戶ID是否具有從/ mnt/data讀取的權限? –

回答

0

其實我發佈的評論了上面,我最終打開錯誤報告。有錯誤,我得到了Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 13484377 bytes) in /opt/share/www/hi.php on line 16所以我加了ini_set("memory_limit","20M");它修復了它

1

整齊的小腳本!我自己爲一個流行的媒體分享網站做了一些媒體處理MP3的MP3,我們一直依靠命令行工具讀取ID3標籤。我從來沒有得到一個腳本來閱讀PHP中的標籤。

無論如何,不​​需要轉移路徑。 PHP沒有這樣正確地將它傳遞給底層操作系統。

我重新創建了/mnt/data/...路徑,並將我自己的MP3文件放在那裏,文件名與您輸入的完全相同。我還在腳本的頂部添加了error_reporting(E_ALL);來打開錯誤報告。該腳本沒有錯誤,並在我的MP3文件中正確顯示了ID3標籤。

它在我看來,該文件不是你認爲它的路徑,或者你的/mnt/data掛載點有問題。你檢查過這些東西嗎?您可以檢查是否PHP能夠看到和使用以下腳本讀取文件:

<?php 
    $path = "/mnt/data/downloads/music/Arctic Monkeys/Who The F Are Arctic Monkeys_ - EP/05 Who The F Are Arctic Monkeys_.mp3"; 
    if (!is_file($path)) { 
     throw new Exception("No such file"); 
    } else if (!is_readable($path)) { 
     throw new Exception("Path exists, but is not readable"); 
    } 
?>