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

发布于 2024-09-07 16:19:35 字数 2220 浏览 1 评论 0原文

我正在尝试获取 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);

i am trying to get a php cli script to go through a folder, get the id3 tags, which are in utf8 in cyrillic and but it in the database. when i execute the script i get the raw utf in the DB fields like "Àëáåíà"

here is the script

<?
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);

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

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

发布评论

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

评论(1

最冷一天 2024-09-14 16:19:36

在我的具体情况下,发生了两件事。

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

In my particular case 2 things were happening.

  1. Putty was somehow disregarding my explicit "use utf-8" settings
  2. The class i was using was not copying id3 tags to "comments_html" wrong. When i print_r-ed the result, i found that by accessing the actual tag i was able to get to the uncorrupted utf-8
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文