Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-07-2005, 06:47 PM   PM User | #1
Hug_It
New to the CF scene

 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hug_It is an unknown quantity at this point
Read ID3 information in different directory

I'm having trouble figuring out how to get this class to read the information it needs from a directory other than the one it's located. The class pulls the ID3 information from MP3 files and works great as long as the files are in the same directory. How would I force it to look at files in a different one. Say in "podcasts/"?
Code:
class CMP3File {
    //properties
    var $title;
    var $artist;
    var $album;
    var $year;
    var $comment;
    var $genre;

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

            $fp = fopen($file, "r");
            fseek($fp, $id_start);
            $tag = fread($fp,3);
            if ($tag == "TAG")
            {
                $this->title    = stripJunk(trim(fread($fp, 30)));
                $this->artist    = stripJunk(trim(fread($fp, 30)));
                $this->album    = stripJunk(trim(fread($fp, 30)));
                $this->year        = stripJunk(trim(fread($fp, 4)));
                $this->comment    = stripJunk(trim(fread($fp, 30)));
                $this->genre    = stripJunk(trim(fread($fp, 1)));
                fclose($fp);
                return true;
            }
            else
            { // No ID3 tag
            	  fclose($fp);
                return false;
            }
        } else //the file doesn't exist
            return false;
    }
Thanks in advance for any ideas or code!
Hug_It is offline   Reply With Quote
Old 06-07-2005, 07:20 PM   PM User | #2
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Tryed:
PHP Code:
$my_file = new CMP3File;
$my_file->getid3('podcasts/my_file.mp3'); 
??
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.

Last edited by marek_mar; 06-07-2005 at 10:31 PM..
marek_mar is offline   Reply With Quote
Old 06-07-2005, 07:36 PM   PM User | #3
Hug_It
New to the CF scene

 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hug_It is an unknown quantity at this point
Well I go through that class in a loop until all the files in the directory are read so I don't want to specify the exact file. Just the directory. Does that make sense? I'm fairly new at this so if I'm not providing you with enough info, I appologize
Hug_It is offline   Reply With Quote
Old 06-07-2005, 07:50 PM   PM User | #4
Hug_It
New to the CF scene

 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hug_It is an unknown quantity at this point
Maybe I should provide a little more code. Basically it goes through and lists all the files and echoes back the tag info in an rss format.

Code:
$dirArray = getDir("podcasts/");
while (list($filename, $filedate) = each($dirArray)AND $maxFeed > 0) {
	$mp3file = new CMP3File;
	$mp3file->getid3 ($filename);
	print "<item>\n";
	echo ("<title>$mp3file->title</title>\n");
	echo ("<link>" . $rootMP3URL . "/". htmlentities(str_replace(" ", "%20", $filename)) ."</link>\n");
	echo ("<description>$mp3file->title - $mp3file->album - $mp3file->artist</description>\n");
	echo ("<pubDate>".date("r",$filedate)."</pubDate>\n");
	echo ("<enclosure url=\"".htmlentities($rootMP3URL)."/". htmlentities(str_replace(" ", "%20", $filename)) ."\" length=\"");
	echo filesize($filename);
	echo ("\" type=\"audio/mpeg\"/>\n");	// Training slash for XML
	print "</item>\n\n";
	$maxFeed--;
}

print "</channel>\n</rss>\n";

// Functions and Classes
function stripJunk ($text) {
// Strip non-text characters
	for ($c=0; $c<strlen($text); $c++) {
		if (ord($text[$c]) >= 32 AND ord($text[$c]) <= 122)
			$outText.=$text[$c];
	}
	return $outText;
}

class CMP3File {
    //properties
    var $title;
    var $artist;
    var $album;
    var $year;
    var $comment;
    var $genre;

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

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

function getDir($mp3Dir) {
// Returns directory as array[file]=date in newest to oldest order
	$dirArray = array();
	$diskdir = "./$mp3Dir/";
	if (is_dir($diskdir)) {
		$dh = opendir($diskdir);
		while (($file = readdir($dh)) != false ) {
			if (filetype($diskdir . $file) == "file" && $file[0]  != ".") {
				if (strrchr(strtolower($file), ".") == ".mp3") {
					$ftime = filemtime($mp3Dir."/".$file);
					$dirArray[$file] = $ftime;
				}
			}
		}
		closedir($dh);
	}
	asort($dirArray);
	$dirArray = array_reverse($dirArray);
	return $dirArray;
}

?>
Hug_It is offline   Reply With Quote
Old 06-07-2005, 10:30 PM   PM User | #5
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
The simplest anwser would be to change
$mp3file->getid3 ($filename);
into
$mp3file->getid3 ('podcasts/' . $filename);
You have to add the path to the file.
BTW there was an error in my last post.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 06-07-2005, 10:39 PM   PM User | #6
Hug_It
New to the CF scene

 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hug_It is an unknown quantity at this point
Brilliant!!!

You know you look at something for soooo long and the most obvious things get overlooked.

Thanks sooo much!!!
Hug_It is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:28 PM.


Advertisement
Log in to turn off these ads.