'%a', 'year' => '%y', 'comment' => '%c', 'album_name' => '%l', 'track' => '%n', 'track_title' => '%t', 'minutes' => '%m', 'seconds' => '%s', ]; // what my filename will looks like $fileFormat = '%artist-%year-%comment-%album_name-%track-M%minutesS%seconds.mp3'; // build format usable for shell and use newline as delimiter $infoParam = implode('\n', $info); // what this can be... print '
'; // scan folders $rdit = new RecursiveDirectoryIterator($directory); foreach (new RecursiveIteratorIterator($rdit) as $file) { // do the job only for files & mp3 ext if($file->isFile() && strtolower($file->getExtension()) == 'mp3') { // get me the id3 tags (mp3info v1 on my debian) $mp3Info = shell_exec('mp3info -p "' . $infoParam . '" ' . $file); // is the info what i expect? $mp3InfoArray = explode("\n", $mp3Info); if(sizeof($mp3InfoArray) != sizeof($info)) { print "check:$file
"; continue; } // make me array with combined $info keys and mp3 info values $mp3InfoArray = array_combine(array_flip($info), $mp3InfoArray); // build new filename $newFile = sprintfa($fileFormat, $mp3InfoArray); // clean a bit $newFile = str_replace([',', '\\', ' ', '/', '%'], '_', $newFile); $newFile = str_replace(['&'], ['and'], $newFile); $newFile = iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($newFile));; $newFile = $file->getPath() . '/' . $newFile; // is the new file equal to the old filename? if(strcmp($file, $newFile) != 0) { // does the newfile already exists? if(file_exists($newFile)) { $newFile = substr_replace($newFile, uniqid('-') . '.mp3',-4, 4); } rename($file, $newFile); // what we did with old to new file print "$file:$newFile
"; flush(); } } } function sprintfa ($format, array $values) { foreach ((array)$values as $k => $v) { $format = str_replace("%$k", $v, $format); } return $format; }