Handy PHP script to add the .mp3 extension to your MP3s

Fahrvergnuugen

I am the law!
If you're like me...in the old days you took the extension off of all of your mp3s so that you had more available characters to name the file something intelligent [thanks to the limit of 32 character file names in os9].

So I had a few thousand mp3s laying around that didn't have the mp3 extension and a few more thousand that did have the extension.

So I wrote this script to add .mp3 to all of the files in a given folder that do not already have an extension. It works recursively, so it doesn't matter how your music dir is setup.


Its setup to do a "test" run first, check everything over and make sure its not going to fubar your music collection! BACK EVERYTHING UP FIRST, I dont want to hear it if something goes terribly wrong and you loose all 568,689 of your MP3s.
PHP:
<?
// Give the absolute path to your music folder
$mp3_path = '/path/to/your/music/folder/';

// This is to stop the script from being executed until you look in here and set it up right
die("i\'m broken, edit this script");

echo "START<br>\n";
$mp3s = GetDirectoryArray(array(), $mp3_path, 0);
echo "\n<br>END";

function GetDirectoryArray($listing, $directory, $count){
      $dummy = $count;

      if($handle = opendir($directory)){
            while($file = readdir($handle)){
            	  //ignore the . and .. dirs
                  if ($file=='.' || $file=='..'){
                        continue;
                  }elseif($h = @opendir($directory.$file."/")){
                        closedir($h);
                        $count = -1;
                        $listing["$file"] = array();
                        GetDirectoryArray(&$listing["$file"],$directory.$file."/", $count + 1);
                  }else{
                        $listing[$dummy] = $file;
                        $dummy = $dummy + 1;

                        if(!preg_match("/.mp3$/",$file) && !preg_match("/(.DS_Store|.FBCIndex|.FBCLockFolder|.FBCSemaphoreFile)/",$file)){
                            $path = $directory.$file;
                            
                            // Comment the next line out when you are ready to actually rename your files
                            echo '<b>rename </b>'.$path.' <b>to</b> '.$path.".mp3<br>\n"; 
                        	
                        	// Uncomment the following IF statement when you're ready to actually rename
                        	//if(!rename($path,$path.".mp3")){
                        	//	echo '<font color="#FF0000">error: '.$path."</font><br>\n";
                        	//}
                        	
                        	$counter++;
                        }
                  }
            }
      }

      closedir($handle);
      return ($listing);
}
?>
 
Back
Top