WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : ID3v2 tag extractor in PHP
Categories : PHP, Tag Extractors Update Picture
Anders Bruun
Date : Mar 01st 2001
Grade : 1 of 5 (graded 1 times)
Viewed : 4420
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Anders Bruun
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?
/*
This file was written by Anders Bruun Olsen (anders@gerf.dk)
It is all released under the GNU GPL license and therefore free
for everybody to use.
I hope that someone will find it useful and perhaps clean it up
and make it a bit smoother..

For those in doubt, it is used by giving a filename (preferebly
with a full path) of an MP3 file containing an ID3v2 tag to
extract_id3v2().
An array is then returned containing all the frames in the tag.
*/

function rem_synchsafe ($tal)
{
        $tal = hexdec(bin2hex($tal));
        $tal1 = floor($tal/256) * 128 + ($tal%256);
        $tal2 = floor($tal1/32768) * 16384 + ($tal1%32768);
        $sluttal = floor($tal2/4194304) * 2097152 + ($tal2%4194304);
        return $sluttal;
};

function get_flags ($fp)
{
        // This function reads the flag bit and puts out something useful...
        $flags = hexdec(bin2hex(fgetc($fp)));
        if (($flags - 128) >= 0) {
                // flag a is set (Unsynchronisation)
                $flags = $flags - 128;
                $flaglist .= "a,";
        }
        if (($flags - 64) >= 0) {
                // flag b is set (Extended Header)
                $flags = $flags - 64;
                $flaglist .= "b,";
        }
        if (($flags - 32) >= 0) {
                // flag c is set (Experimental indicator)
                $flags = $flags - 32;
                $flaglist .= "c,";
        }
        if (($flags - 16) >= 0) {
                // flag d is set (Footer present)
                $flags = $flags - 16;
                $flaglist .= "d,";
        }
if (($flags - 8) >= 0) {
// flag e is set (Unused in v2.4.0)
$flags = $flags - 8;
$flaglist .= "e,";
}
if (($flags - 4) >= 0) {
// flag f is set (Unused in v2.4.0)
$flags = $flags - 4;
$flaglist .= "f,";
}
if (($flags - 2) >= 0) {
// flag g is set (Unused in v2.4.0)
$flags = $flags - 2;
$flaglist .= "g,";
}
if (($flags - 1) >= 0) {
// flag h is set (Unused in v2.4.0)
$flags = $flags - 1;
$flaglist .= "h,";
}
        if (strlen($flaglist) > 0)
        {
                $flaglist = substr($flaglist,0,strlen($flaglist)-1);
        }
        return $flaglist;
};

function get_id3pos ($filename)
{
        // This function will return the position of the ID3v2 tag if found
        $fp = fopen($filename,"r");
        while(!feof($fp)){
                $char = fgetc($fp);
                if($char == "I") {
                        $char = fgetc($fp);
                        if ($char == "D") {
                                $char = fgetc($fp);
                                if ($char == "3") {
                                        $id3pos = ftell($fp);
                                }
                        }
                }
        }
        fclose($fp);
        if (!$id3pos) {
                return false;
        } else {
                return $id3pos;
        }
};

function goto_id3pos ($filename,$id3pos)
{
        // This function will open the file, go to the ID3v2 tag position and return the filepointer
        $fp = fopen($filename,"r");
        if (fseek($fp,$id3pos) != 0) {
                die ("Could not go to ID3 tag\n");
        }
        return $fp;
};

function get_id3_version ($fp)
{
        // This function gets the ID3v2 version from the file open at the filepointer and returns it
        $ver1 = hexdec(bin2hex(fgetc($fp)));
        $ver2 = hexdec(bin2hex(fgetc($fp)));
        $version = "ID3v2." . $ver1 . "." . $ver2;
        return $version;
};

function get_tagsize ($fp)
{
        // This function gets the tagsize converts it from a synchsafe int to a normal int and returns it
        for ($i = 0; $i < 4; $i++) {
                $tagsize .= fgetc($fp);
        }
        $tagsize = rem_synchsafe($tagsize);
        return $tagsize;
};

function extract_id3v2($filename)
{
        // Find the position of the ID3v2 tag
        
        if (!$id3pos = get_id3pos($filename)) {
                die("No ID3v2 tag found in $filename\n");
        };
        
        // go to id3v2 tag in file
        
        $fp = goto_id3pos($filename,$id3pos);
        
        // Get the version
        
        $id3ver = get_id3_version($fp);
        
        $id3flags = get_flags($fp);
        
        $tagsize = get_tagsize($fp);
        $tagsize_left = $tagsize;
        while ($tagsize_left > 1) {
                unset($frameID);
                unset($framesize);
                unset($frame);
                for ($i = 0; $i < 4; $i++) {
                        $frameID .= fgetc($fp);
                }
                for ($i = 0; $i < 4; $i++) {
                        $framesize .= fgetc($fp);
                }
                $framesize = rem_synchsafe($framesize);
                if ($framesize >= 1)
                {
                        $tagsize_left = $tagsize_left - ($framesize + 10);
                        $flag1 = get_flags($fp);
                        $flag2 = get_flags($fp);
                        $encoding = hexdec(bin2hex(fgetc($fp)));
                        if ($encoding <= 3) {
                                // This is the encoding bit
                                for ($i = 0; $i < ($framesize - 1); $i++) {
                                        $frame .= fgetc($fp);
                                }
                        } else {
                                // This was not an encoding bit, it is part of the frame content
                                $frame = $encoding;
                                for ($i = 0; $i < $framesize; $i++) {
                                        $frame .= fgetc($fp);
                                }
                        }
                }
                else
                {
                        $frameID = "Padding";
                        $tagsize_left = 0;
                }
                if ($frameID == "SYLT" OR $frameID == "USLT" OR $frameID == "USER" OR $frameID == "COMM")
                {
                        $frame = substr($frame,3,strlen($frame));
                }
                $framearray["$frameID"] = $frame;
        };
        fclose($fp);
        return $framearray;
}
?>



Parse a HTML-document for a certain tag and display its content and attributes.
Categories : HTML and PHP, PHP, Tag Extractors
Parsing html tags with php. Get an array from this function
Categories : PHP, HTML and PHP, Arrays, Tag Extractors
MP3 Tag reader module.
Categories : PHP, Tag Extractors
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Retrieve text from table and email to your e- address in pipe delimited format.
Categories : PHP, MySQL
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate.
Categories : Authentication, PHP
very simple ftp class
Categories : PHP, PHP Classes, FTP
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
Function to remember password
Categories : PHP, Authentication, Personalization and Membership
Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality.
Categories : PHP, GD image library, Graphics
readline -- Reads a line
Categories : PHP, PHP Functions, Readline
a function that builds an HTML select list from any mysql table.
Categories : PHP, MySQL, HTML and PHP
Math operations on big numbers
Categories : PHP, Math.
 Anders Olsen wrote :538
If you run into trouble with PHP timeouts it is because of the loop that locates the ID3v2 tag position. As most ID3v2 tags are found in the beginning of files I have modified the code to skip that loop and just read the tag from the start.
This means that files where the tag is not located in the beginning is not supported.
The newest version of this code can be found at:
http://www.elffan.com/id3v2