Location: PHPKode > projects > Axis Groupware > musiclib/unused/tunez/rescan.php
<?php error_reporting(1);
// Select a library from new config variables
// echo "Select a library to rescan: <hr>";
// Delete all the existing entries where the path = the selected library

if ($dbconfig["sqlserver"] = "mysql" || $dbconfig["sqlserver"] = "pgsql") {  
  $result=dbQuery("DELETE FROM mp3data");
}

// starting from the local_storage array...
// get all subdirectories recursively

function recurs ($bspath, $nxpath) {
  $dirhandle = dir($nxpath);
  while ($direct = readdir($dirhandle->handle)):
    $nxpath=$bspath . "/" . "." . "/" . $direct;
    if (is_dir($nxpath) && $direct!="." && $direct!="..") {
      $nxpath = str_replace("/./","/",$nxpath);
      global $dirlist;
      $dirlist[count($dirlist)]=$nxpath;
      recurs ($nxpath, $nxpath);
      }
  endwhile;
}

for ($qz = 0; $qz <= count($config[local_storage])-1; $qz++) {
$path=$config[local_storage][$qz];
  recurs($path, $path);
}

closedir($dirhandle->handle);
unset ($handle);

// For each path in the local_storage array...
for ($qz = 0; $qz <= count($dirlist)-1; $qz++) {
    // Get a list of files and load into an array
    $dirhandle = dir($dirlist[$qz]);
    $path = $dirhandle->path;  
    // Grab the files and read the tags
    while ($file = readdir($dirhandle->handle)):
        if ($file != "." && $file != "..") {
            if (strtoupper(substr("$file",strlen("$file")-4)) == ".MP3") { 
                $filelist[count($filelist)] = $file;
            }
        }
    endwhile;

    closedir($dirhandle->handle);
  
    // Reset the file handle
    unset($handle);

    //Set up an array to store tags in

    $mp3data = array(
        "tag",
        "filename",
        "size",
        "artist",
        "title",
        "genre",
        "album",
        "comment",
        "year"
    );

    // Set up an array of genre names
    $genrename =array(
        "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
        "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
        "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno",
        "Industrial", "Alternative", "Ska", "Death Metal", "Pranks",
        "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal",
        "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental",
        "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise",
        "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
        "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic",
        "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk",
        "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta",
        "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American",
        "Cabaret", "New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer",
        "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro",
        "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk/Rock",
        "National Folk", "Swing", "Bebob", "Latin", "Revival", "Celtic",
        "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock",
        "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band",
        "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson",
        "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
        "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango",
        "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul",
        "Freestyle", "Duet", "Punk Rock", "Drum Solo", "A capella",
        "Euro-House", "Dance Hall"
    );

    //Go through the list and extract the tags

    for ($iz = 0; $iz <= count($filelist)-1; $iz++) {
        //DEBUG LINE
        //echo "Opening $path/$filelist[$iz]";
        // Open the file
        $filehandle = fopen("$path/$filelist[$iz]","r");
        $size = filesize("$path/$filelist[$iz]");

        //Put the filename in the array
        $mp3data["filename"] = "$path/$filelist[$iz]";
 
        //Put the size in the array
        $mp3data["size"] = $size;
 
        // Get the last 128 bytes and see if there's a TAG there
        $seek = fseek($filehandle, $size-128);
        $tag = fgets($filehandle,128);
        // Grab last char for the Genre
        // This can get confused for EOF with fgets
        $seek = fseek($filehandle, $size-1);
        $gen = fgetc($filehandle);
   
        if (strstr("$tag", "TAG") || strstr("$tag", "tag")) {

            // Extract the tag data and close the file
            // This is the correct order... 
            $mp3data["title"] = substr($tag,3, 30);
            $mp3data["artist"] = substr($tag,33, 30);
            $mp3data["album"] = substr($tag,63, 30);
            $mp3data["year"] =  substr($tag,93, 4);
            $mp3data["comment"] = substr($tag,97,30);
            $mp3data["genre"] = $genrename[ord($gen)];
            // Gotta fix them darn ' chars and strip spaces from year    
            $mp3data["filename"] = str_replace("'","''",$mp3data["filename"]);  
	    $mp3data["title"] = str_replace("'","",$mp3data["title"]);
	    $mp3data["artist"] = str_replace("'","",$mp3data["artist"]);
            $mp3data["album"] = str_replace("'","",$mp3data["album"]);
	    $mp3data["year"] = str_replace(" ","",$mp3data["year"]);
            $mp3data["year"] = str_replace("_","",$mp3data["year"]);
            $mp3data["comment"] = str_replace("'","",$mp3data["comment"]);

	    fclose($filehandle);
      }
    
      else {
          //just use the filename for title if no tags
          $mp3data["title"] = str_replace("'","''",$filelist[$iz]);
          fclose($filehandle);
      }

      // Turn it into SQL and put it in the database
     
      if ($dbconfig["sqlserver"] = "mysql" || $dbconfig["sqlserver"] = "pgsql"){ 
        $insertquery = "INSERT INTO mp3data (tag,filename, size";
          if ($mp3data["artist"] != "") { $insertquery .= ",artist"; }
          if ($mp3data["title"] != "") { $insertquery .= ",title"; }
          if ($mp3data["genre"] != "") { $insertquery .= ",genre"; }
          if ($mp3data["album"] != "") { $insertquery .= ",album"; }
          if ($mp3data["comment"] != "") { $insertquery .= ",comment"; }
          if ($mp3data["year"] != "") { $insertquery .= ",year"; }
        $insertquery .= ") VALUES ('1','" . $mp3data["filename"] . "','" . $mp3data["size"] . "'";
          if ($mp3data["artist"] != "") { $insertquery .= ",'" . $mp3data["artist"] . "'"; }
          if ($mp3data["title"] != "") { $insertquery .= ",'" . $mp3data["title"] ."'"; }
          if ($mp3data["genre"] != "") { $insertquery .= ",'" . $mp3data["genre"] ."'"; }
          if ($mp3data["album"] != "") { $insertquery .= ",'" . $mp3data["album"] ."'"; }
          if ($mp3data["comment"] != "") { $insertquery .= ",'" . $mp3data["comment"] ."'"; }
          if ($mp3data["year"] != "") { $insertquery .= ",'" . $mp3data["year"] ."'"; }
        $insertquery .= ");";
        $result=dbQuery($insertquery);
      }
        //Reset the mp3data so if the next file doesn't have a tag, we're clean...
        unset($mp3data);

    }

    unset($filelist);
    unset($dirhandle);

}

?>
Return current item: Axis Groupware