move_music_to_singer_folder.php
<?php
/**
* 对一个文件夹中的所有歌曲文件移动到对应的歌手目录中, 歌手按照拼音首字母作为第一级目录.
* 同时,如果歌手和歌曲中间有空格,则去除空格.
* 例如: "周杰伦 - 稻香.mp3" 移动到 "Z/周杰伦/周杰伦-稻香.mp3"
*/
try {
require_once 'music_common.php';
$folder = 'D:\MP3';
$folder_ok = 'D:\MP3_OK';
$file_list = readAllFiles($folder);
//PE($file_list);
$count = count($file_list);
foreach ($file_list as $k => $v) {
$original_filepath = $folder . DIRECTORY_SEPARATOR . $v;
if(is_dir($original_filepath)){
echo "跳过文件夹 \n";
continue;
}
$item = explode('-', $v);
$singer = trim($item[0]);
unset($item[0]);
//歌手名称的拼音首字母
$first_str = getFirstCharter($singer);
if (!$first_str) {
$first_str = '0';
}
//多个歌手的情况下,只将第一个歌手作为文件夹
$singer_first_str = $singer;
$singer_arr = [];
if (strstr($singer, '、')) {
$singer_arr = explode('、', $singer);
} elseif (strstr($singer, '&')) {
$singer_arr = explode('&', $singer);
}
if (!empty($singer_arr)) {
//如果歌手名称过长,只保留前3个
if (count($singer_arr) > 3) {
$singer_arr = array_slice($singer_arr, 0, 3);
}
$singer_first_str = $singer_arr[0];
$singer = implode('&', $singer_arr);
}
$new_path = $folder_ok . DIRECTORY_SEPARATOR . $first_str . DIRECTORY_SEPARATOR . $singer_first_str;
if (!is_dir($new_path)) {
mkdir($new_path, 0777, true);
}
$song_name = $singer . '-' . trim(implode('-', $item));
$new_filepath = $new_path . DIRECTORY_SEPARATOR . $song_name;
//PE($original_filepath . ' --> ' . $new_filepath);
//执行重命名
rename($original_filepath, $new_filepath);
$num = $k + 1;
if ($k % 100 == 0) {
echo "第 {$num} / {$count} 个: {$v} 完成. \n";
}
}
} catch (\Exception|\Error $e) {
PE("Error: " . $e->getMessage());
}
music_common.php
<?php
date_default_timezone_set('PRC');
/**
* 获取一个目录下的所有文件,不包含路径
*/
function readAllFiles($folder)
{
$handler = opendir($folder);
$res = array();
while (($filename = readdir($handler)) !== false) {
if ($filename != "." && $filename != "..") {
$res[] = $filename;
}
}
closedir($handler);
return $res;
}
function getFirstCharter($str)
{
if (empty($str)) {
return '';
}
$fchar = ord($str{0});
if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
$s1 = iconv('UTF-8', 'gb2312', $str);
$s2 = iconv('gb2312', 'UTF-8', $s1);
$s = $s2 == $str ? $s1 : $str;
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if ($asc >= -20319 && $asc <= -20284) return 'A';
if ($asc >= -20283 && $asc <= -19776) return 'B';
if ($asc >= -19775 && $asc <= -19219) return 'C';
if ($asc >= -19218 && $asc <= -18711) return 'D';
if ($asc >= -18710 && $asc <= -18527) return 'E';
if ($asc >= -18526 && $asc <= -18240) return 'F';
if ($asc >= -18239 && $asc <= -17923) return 'G';
if ($asc >= -17922 && $asc <= -17418) return 'H';
if ($asc >= -17417 && $asc <= -16475) return 'J';
if ($asc >= -16474 && $asc <= -16213) return 'K';
if ($asc >= -16212 && $asc <= -15641) return 'L';
if ($asc >= -15640 && $asc <= -15166) return 'M';
if ($asc >= -15165 && $asc <= -14923) return 'N';
if ($asc >= -14922 && $asc <= -14915) return 'O';
if ($asc >= -14914 && $asc <= -14631) return 'P';
if ($asc >= -14630 && $asc <= -14150) return 'Q';
if ($asc >= -14149 && $asc <= -14091) return 'R';
if ($asc >= -14090 && $asc <= -13319) return 'S';
if ($asc >= -13318 && $asc <= -12839) return 'T';
if ($asc >= -12838 && $asc <= -12557) return 'W';
if ($asc >= -12556 && $asc <= -11848) return 'X';
if ($asc >= -11847 && $asc <= -11056) return 'Y';
if ($asc >= -11055 && $asc <= -10247) return 'Z';
return null;
}
function P($arr)
{
print_r($arr);
echo "\n";
}
function PE($arr)
{
P($arr);
exit;
}