php在cli脚本中编码读取id3标签的问题

时间:2010-06-29 15:24:47

标签: php mysql encoding utf-8 id3

我正在尝试获取一个php cli脚本来浏览文件夹,获取id3标签,这些标签位于西里尔语的utf8中,但它在数据库中。当我执行脚本时,我得到了"Àëáåíà"

等数据库字段中的原始utf

这是脚本

<?
set_time_limit(0);

include('classes/adodb5/adodb.inc.php');
include ('classes/id3/getid3.php');

$ftpdir = "/radio/unprocessed/";
$processeddir = "/radio/music";

//set up the database

$conn = &ADONewConnection('mysql');
$conn->PConnect('localhost','root', '**********','radio');

$utf = $conn->Execute("SET NAMES 'UTF8';");
$charset = $conn->Execute("CHARSET UTF8;");

//function for processing the actual file
function processmp3($fn, $folder, $conn){
                        $getID3 = new getID3;
                        $ThisFileInfo = $getID3->analyze($folder.$fn);
                        //this is needed to consolidate all tag formats
                        getid3_lib::CopyTagsToComments($ThisFileInfo);
                        if (array_key_exists('artist', $ThisFileInfo['comments_html'])&& array_key_exists('artist', $ThisFileInfo['comments_html'])){
                                $artist=($ThisFileInfo['comments_html']['artist'][0]);
                                $title=($ThisFileInfo['comments_html']['title'][0]);
                        }else{$artist ='not defined'; $title="not defined";}
                        //random name
 //random name
                        $rand_name = md5(time()).rand(1,1000).".mp3";
                        //movefile
                        //rename($folder.$fn,'/radio/music/'.$rand_name);
                        //put in DB
                        $insert = $conn->Execute('INSERT INTO unprocesseds VALUES("","'.$artist.'","'.$title.'","'.$rand_name.'","'.$fn.'");');
                        }


//cyccle through contents
if($handle = opendir($ftpdir)){
        while(false !== ($file = readdir($handle))){
                $type = mime_content_type($ftpdir.$file);
                if ($type=='audio/mpeg'){processmp3($file, $ftpdir, $conn);}
                else {
                        if(is_file($ftpdir.$file)){unlink($ftpdir.$file);}
                        }
                }


        }

closedir($handle);

1 个答案:

答案 0 :(得分:0)

在我的特定情况下,发生了两件事情。

  1. Putty不知何故忽视了我明确的“使用utf-8”设置
  2. 我正在使用的类没有将id3标签复制到“comments_html”错误。当我print_r-ed结果时,我发现通过访问实际标签我能够获得未损坏的utf-8