View Full Version : Write a Directorys Content to XML file
ashtray
02-24-2009, 01:56 PM
Hey, I have a flash mp3 player that reads an XML (.xspf) file for the playlist, how would I generate the XML file from the music directory? all the mp3 files are in the directory /var/www/music/ and the XML file is in /var/www/listen/playlist.xspf
This is how the structure of the XML file should be:
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="0" xmlns = "http://xspf.org/ns/0/">
<trackList>
<track>
<location>/music/song1.mp3</location>
<image>image URL can be blank for now</image>
<annotation>file name for song1 here</annotation>
</track>
<track>
<location>/music/song2.mp3</location>
<image>image URL can be blank for now</image>
<annotation>file name for song2 here</annotation>
</track>
</trackList>
</playlist>
Thanks for any help
ashtray
02-25-2009, 11:14 AM
Ok, I've stripped down this (http://codingforums.com/showpost.php?p=767904&postcount=2) PHP code (probably can be stripped down more if i knew what else isnt needed). It outputs exactly what I need to make the XML file but it only outputs it to the current page and dont write it to an XML (xspf) file.
I need to add something like:
echo $doc->saveXML();
$doc->save("playlist.xspf")
But I dont know how or where. Any help please?
<?php
$myfeed = new RSSFeed();
// Open the current directory (or specify it) and grab only .mp3 and .wma files ...
$dir = opendir ("/var/www/music/");
while (false !== ($file = readdir($dir))) {
if (strpos($file, '.mp3',1)||strpos($file, '.wma',1) ) {
$myfeed->SetItem("http://192.168.0.10/music/$file", "$file", "");
}
}
// Output the XML File ... you could write it to the directory instead.
echo $myfeed->output();
class RSSFeed {
// VARIABLES
// channel vars
var $channel_url;
var $channel_title;
// items
var $items = array();
var $nritems;
// FUNCTIONS
// constructor
function RSSFeed() {
$this->nritems=0;
$this->channel_url='';
$this->channel_title='';
}
// set channel vars
function SetChannel($url, $title, $description, $lang, $copyright, $creator, $subject) {
$this->channel_url=$url;
$this->channel_title=$title;
}
// set item
function SetItem($url, $title, $description) {
$this->items[$this->nritems]['url']=$url;
$this->items[$this->nritems]['title']=$title;
$this->nritems++;
}
// output feed
function Output() {
$output = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$output .= '<playlist version="0" xmlns = "http://xspf.org/ns/0/">'."\n";
$output .= '<trackList>'."\n";
for($k=0; $k<$this->nritems; $k++) {
$output .= '<track>'."\n";
$output .= '<location>'.$this->items[$k]['url'].'</location>'."\n";
$output .= '<image></image>'."\n";
$output .= '<annotation>'.$this->items[$k]['title'].'</annotation>'."\n";
$output .= '</track>'."\n";
};
$output .= '</trackList>'."\n";
$output .= '</playlist>'."\n";
return $output;
}
};
?>
bluewalrus
02-25-2009, 01:53 PM
set it to write to the xml file with this
$info ="filename.xml";
$writeit = fopen($info, 'a');
$wrote = fwrite($writeit, $output);
fclose($writeit);
This will keep adding to the xml file and xml file will need permissions to be changed to 777.
ashtray
02-25-2009, 03:17 PM
Thanks that works, but every time you visit the page or refresh, the playlist is duplicated on top of the other (in the same XML file).
When I add a new song to the music folder, it wont get updated in the player.
So I guess the question is: How do you make the PHP script only add the new songs in the music directory, or remove what it wrote before and write it again with the updated files.
ashtray
02-25-2009, 04:21 PM
Nevermind, just changed the 'a' to 'w' and it works, thanks.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.