filesize() stat 函数获取 MP3 文件的 ID3 标签失败

发布于 2024-11-02 01:00:28 字数 2943 浏览 4 评论 0原文

它适用于本地文件,但不适用于外部域。这是我的代码(我从 获得http://www.barattalo.it/2010/02/22/reading-mp3-informations-with-php-id3-tags/):

<?
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, 10+$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['Length'] = 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;
}
$song = tagReader("file.mp3");
echo $song[Title];
?>

It works with local files but not with external domains. Here's my code (which I got from http://www.barattalo.it/2010/02/22/reading-mp3-informations-with-php-id3-tags/):

<?
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, 10+$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['Length'] = 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;
}
$song = tagReader("file.mp3");
echo $song[Title];
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2024-11-09 01:00:28

它仅适用于本地文件。您无法将远程 mp3 文件作为此函数的参数传递,这是毫无意义的。文件使用 fopen() 打开,因此它们必须是本地文件。 filesize() 也是如此。

  $fsize = filesize($file);
  $fd = fopen($file,"r");

您到底想用这个函数加载什么? “它不适用于外部域”是什么意思?

此行仅适用于与此脚本存储在同一目录中的 file.mp3

  $song = tagReader("file.mp3");

如果您无法读取本地文件,则应检查该文件的读取权限。

filesize() 可能不起作用的原因相同。

建议:您应该对字母数字数组索引使用引号...

  echo $song['Title']; // or 
  echo $song["Title"];

而不是

  echo $song[Title];

“是的,它可能有效”,但您仍然不应该这样做。我建议使用单引号来避免在某些特定情况下评估变量名称(作为数组索引,例如 $array['$index'])。

It works only with local files. You cant pass remote mp3 file as parameter of this function and that is senseless. Files open with fopen() so they must be local files. Same thing for filesize().

  $fsize = filesize($file);
  $fd = fopen($file,"r");

What exactly you want to load with this function? What do you mean with "It doesn't work with external domains"?

This line should work only with file.mp3 stored in same directory as this script.

  $song = tagReader("file.mp3");

If you can't read local file, than you should check READ permission for that file.

Same reason why filesize() probably don't work.

Suggestion: You should use quotes for alphanumeric array indexes...

  echo $song['Title']; // or 
  echo $song["Title"];

instead of

  echo $song[Title];

Yes, it probably works, but still, you should not do that. I suggest using single quotes to avoid evaluation of variable names in some specific cases (as array indexes such as $array['$index']).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文