1

php新手。

我使用 php 脚本生成播放列表,列表中的第一个歌曲条目对于所有字段(艺术家、标题、长度、文件名)都是空白的。为什么?

这是脚本

 <?php

 require_once('getid3/getid3.php');

 $dir = 'mp3';
 $file_type = 'mp3';

 $play_list = '<?xml version="1.0" encoding="utf-8"?><config>';


 if (is_dir($dir)) {

 if ($dh = opendir($dir)) {

 while (($file = readdir($dh)) !== false) {

 if ($file != '.' && $file != '..') {

 $name_array = explode('.', $file);


 if ($name_array[1] == $file_type) {

 $play_list .= '<song>';

 $file = "$dir/$file";

 $getID3 = new getID3;

   $ThisFileInfo = $getID3->analyze($file);

  getid3_lib::CopyTagsToComments($ThisFileInfo);


         $play_list .= '<artist>'.$ThisFileInfo['comments_html']['artist'][0] . '</artist>';

         $play_list .= '<title>'. $ThisFileInfo['tags']['id3v2']['title'][0]. '</title>';

  $new_playtime = ereg_replace("[^A-Za-z0-9]", "", $ThisFileInfo['playtime_string'] );

  $play_list .= '<length>'.$new_playtime . '</length>';
  $play_list .= '<fileName>'.$file.'</fileName>';



 $play_list .= '</song>';

 }

 }

 }

 closedir($dh);

 $play_list .= '</config>';

 echo "$play_list";

 }

 }


     ?>

这就是输出的样子。

     <?xml version="1.0" encoding="utf-8"?>
    <config>

     <song><artist></artist><title></title><length></length><fileName>mp3/air - all i need.mp3</fileName></song>

<song><artist>Air</artist><title>Remember</title><length>234</length><fileName>mp3/air - remember.mp3</fileName></song>

<song><artist>Air</artist><title>You Make It Easy</title><length>401</length><fileName>mp3/air - you make it easy.mp3</fileName></song>

<song><artist>Ian Brown</artist><title>Dolphins Were Monkeys (Single Version)</title><length>258</length><fileName>mp3/Ian Brown - Dolphins Were Monkeys.mp3</fileName></song>

<song><artist>Ian Brown</artist><title>F.E.A.R.</title><length>431</length><fileName>mp3/Ian Brown - Fear.mp3</fileName></song>

<song><artist>Ian Brown</artist><title>Keep What Ya Got</title><length>348</length><fileName>mp3/Ian Brown - Keep What Ya Got.mp3</fileName></song>

<song><artist>Ian Brown</artist><title>My Star</title><length>509</length><fileName>mp3/Ian Brown - My Star.mp3</fileName></song>

<song><artist>Ian Brown</artist><title>Time Is My Everything</title><length>354</length><fileName>mp3/Ian Brown - Solarized - 02 - Time Is My Everything.mp3</fileName></song></config>      
4

2 回答 2

3

文件名字段不为空: <song><artist></artist><title></title><length></length><fileName>mp3/air - all i need.mp3</fileName></song>

也许只是因为没有为“我需要的所有.mp3”设置 ID3 标签?

PS:你应该使用pathinfowithPATHINFO_EXTENSION而不是 an explode,因为如果一首歌包含多个点,它将不会被匹配。

于 2010-02-12T18:20:58.297 回答
0

尝试使用 glob() 获取文件。 http://us.php.net/manual/en/function.glob.php

$files = glob($dir . '/*.mp3');

foreach ($files as $file) {
...
}

使用 pathinfo() 获取文件扩展名信息。 http://php.net/manual/en/function.pathinfo.php

虽然,如果您使用上面的 glob() 函数,则不必这样做,因为 *.mp3 已经过滤了 mp3 文件。

于 2010-02-12T18:32:37.520 回答