Results 1 to 5 of 5

Thread: Write a Directorys Content to XML file

  1. #1
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a Directorys Content to XML file

    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:

    Code:
    <?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

  2. #2
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, I've stripped down this 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:
    PHP Code:
    echo $doc->saveXML();
    $doc->save("playlist.xspf"
    But I dont know how or where. Any help please?

    PHP Code:
    <?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
        } 
    }; 

    ?>

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    set it to write to the xml file with this

    PHP Code:
    $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.

  4. #4
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  5. #5
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nevermind, just changed the 'a' to 'w' and it works, thanks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •