I wrote this code to sort out music I had ripped from an old mp3 player, where all the file names had been changed to MV34P.mp3 or the likes. The code simply takes the id3 tags from the file and then renames the file using the supplied "Artist - Title.mp3"
The code only works for ID3v1.
<?php
include ('functions.php');
connectDB('ipodmusic');
set_time_limit(0);
if ($handle = opendir("musicloop/")) {
echo ("<strong>Directory opened successfully.</strong>");
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$filesize = filesize("musicloop/" . $file);
$mp3 = fopen("musicloop/" . $file, "r");
fseek($mp3, -128, SEEK_END);
$tag = fread($mp3, 3);
if ($tag == "TAG") {
$data["Title"] = trim(fread($mp3, 30));
$data["Artist"] = trim(fread($mp3, 30));
$data["Album"] = trim(fread($mp3, 30));
}
fclose($mp3);
$newName = $data['Artist'] . " - " . $data['Title'];
$renaming = rename("musicloop/" . $file, "musicloop/" . $newName . ".mp3");
$query = ("INSERT INTO `test`.`testing` (`title`, `artist`, `album`, `location`) VALUES ('" .
$data['Title'] . "', '" . $data['Artist'] . "', '" . $data['Album'] .
"', 'musicloop/" . $newName . ".mp3');");
$insert = mysql_query($query);
}
}
echo ("</br><strong>Operation Completed successfully!</strong>");
}
?>