<?php

$this_file = $_SERVER["PHP_SELF"];
$this_file = explode("/", $_SERVER["PHP_SELF"]);
$this_file = $this_file[count($this_file) - 1];

$directory = $_SERVER["HTTP_HOST"] . str_replace($this_file, "", $_SERVER["PHP_SELF"]);
$directory1 = str_replace(" ", "%20", $directory);
$directory2 = str_replace("&", "&amp;", $directory1);

header("Content-Type: text/xml");

if ($firsthandle = opendir('./')) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($firstfile = readdir($firsthandle))) {
                        if (($firstfile != '.') && ($firstfile != '..') && ($firstfile != $this_file) && ($firstfile != 'cover.jpg')){
 				$firsttag = id3_get_tag( $firstfile );
	}}closedir($firsthandle);}
echo '<?xml version="1.0" encoding="iso-8859-1"?><rss version="2.0"><channel><title>'.$firsttag['album']. '</title><link>http://'.$directory2.'</link><description>'.$firsttag['artist'].'</description><language>en-us</language><image>
<url>http://';
echo $directory2;
echo 'cover.jpg</url>
<title>Cover</title>
<link>http://'.$directory2.'</link>
<description>Music Feed by panvamp for PSP</description>
</image>';

if ($handle = opendir('./')) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
			if (($file != '.') && ($file != '..') && ($file != $this_file) && ($file != 'cover.jpg')){	
				echo '<item>';				
			        $tag = id3_get_tag( $file );
				echo '<title>';
				echo  str_replace("&", "and", $tag['title']);
				echo '</title>';
			$file1 = str_replace(" ", "%20", $file);
			$file2 = str_replace("&", "&amp;", $file1);
				echo '<link>';
				echo 'http://' . $directory2 . $file2;
				echo '</link>';
				echo '<description>';
				$descr = "{$tag['album']} - {$tag['title']}";
				$descr1 = str_replace("&", "and", $descr);
				echo $descr1;	
				echo '</description>';
				echo '<author>';
				echo  str_replace("&", "and", $tag['artist']);
				echo '</author>';
				echo '<pubDate>';
				echo date(DATE_RFC822);
				echo '</pubDate>'; 
				echo '<enclosure url="http://' . $directory2 . $file2;
				echo '" type="audio/mpeg" />';
				echo '</item>';
			}
    }

    closedir($handle);
}

echo '</channel></rss>';
?>

