Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about ID3 tag reading script

Status
Not open for further replies.

frummel

Technical User
Jun 21, 2002
91
NL
I use the following script:

<?PHP

include (&quot;CMP3File.php&quot;);


$filename = &quot;mp3/01.mp3&quot;;

$mp3file = new CMP3File;
$mp3file->getid3 ($filename);


echo (&quot;Title: $mp3file->title<br>\n&quot;);
echo (&quot;Artist: $mp3file->artist<br>\n&quot;);
echo (&quot;Album: $mp3file->album<br>\n&quot;);
echo (&quot;Year: $mp3file->year<br>\n&quot;);
echo (&quot;Comment: $mp3file->comment<br>\n&quot;);
echo (&quot;Genre: &quot; . Ord($mp3file->genre) . &quot;<br>\n&quot;);

?>


and CMP3File.php:

<?php

class CMP3File
{ // CMP3File declaration

//properties
var $title;
var $artist;
var $album;
var $year;
var $comment;
var $genre;


function getid3 ($file)
{ // read the ID3 tag from an MP3 file
if (file_exists($file))
{ //after verifying the file exists,
$id_start = filesize($file) - 128;

$fp = fopen($file, &quot;r&quot;);
fseek($fp, $id_start);
$tag = fread($fp,3);
if ($tag == &quot;TAG&quot;)
{
$this->title = fread($fp, 30);
$this->artist = fread($fp, 30);
$this->album = fread($fp, 30);
$this->year = fread($fp, 4);
$this->comment = fread($fp, 30);
$this->genre = fread($fp, 1);
fclose($fp);
return true;
}
else
{ //there is no ID3 tag in the file
fclose($fp);
return false;
}
} else //the file doesn't exist
return false;
}
}

?>


Everything is right, but when displaying the genre, I get a number. Like this:

Title: We're At The Top Of The World
Artist: The Juliana Theory
Album: Emotion Is Dead
Year: 2000
Comment: Good Song!
Genre: 17

Each genre has a number. Rock = 17, Hip Hop = 125, etcetera.

What I need is: if the script puts out number 17, it has to display 'ROCK', and if it puts out 125, it has to display 'HIPHOP'.

Does anyone know how I can do all this? There's about 150 genre's so keeping the scriptsize to a minimum would be preferable.
 
You could create a file which is formatted something like:

17=Rock
125=Hip Hop
.
.
.

And write a function which opens the file, reads each line, splits the line at the &quot;=&quot;, and keeps doing so until it finds the value associated with the number you need. If you are outputting multiple records, you could also do all of the above once, creating an array where the subscripts are the numbers and the values are the text.

You could also do the same thing in a database. ______________________________________________________________________
TANSTAAFL!
 
I would make an external file containing an array...
<?
...
genre[17]='rock';
...
genre[125]='hip hop';
...
?>
then just include it in the begining of your script. -gerrygerry

Standard response to one of my posts:
&quot;No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul.&quot;
 
So I did the following in the script:

<?PHP

include (&quot;CMP3File.php&quot;);
include (&quot;genres.php&quot;);

$filename = &quot;mp3/01.mp3&quot;;

$mp3_genre = Ord($mp3file->genre);
$mp3file = new CMP3File;
$mp3file->getid3 ($filename);


echo (&quot;Title: $mp3file->title<br>\n&quot;);
echo (&quot;Artist: $mp3file->artist<br>\n&quot;);
echo (&quot;Album: $mp3file->album<br>\n&quot;);
echo (&quot;Year: $mp3file->year<br>\n&quot;);
echo (&quot;Comment: $mp3file->comment<br>\n&quot;);
echo (&quot;Genretest: $genre_no[$mp3_genre]<br>\n&quot;);
(This outputs the number '0')
echo (&quot;Genre: &quot; . Ord($mp3file->genre) . &quot;<br>\n&quot;);
(This outputs the number '17')


?>


I also tried to use arrays in genres.php. It looks something like this:


<?php

$genre_no[0]=&quot;Null-Genre (script outputs 0)&quot;;
$genre_no[17]=&quot;Rock&quot;;

?>


The array works, because the script outputs 'Genretest: Null-Genre (script outputs 0)'

What am I doing wrong here that makes $genre_no[$mp3_genre] output '0'?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top