0

Hi I have just recently made a FLASH AS3 XML playlist player which works great apart from the part where I have to type all the XML out. I have had some help with someone to compile this code that works great, but I want to be able to save the XML to file and not just read of it. And to update itself if the number of .mp3s in the given folder increases.

    require_once('getid3/getid3.php');
    $dir = $_GET['folder'];
    $xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $xmlBody .= "<XML>";
    $getID3 = new getID3;
    $DirectoryToScan = 'SOUNDS';
    $dirHandle = opendir($dir);
    $strIsNull = "not found";
    $i = 0;
    while (($file = readdir($dirHandle)) !== false) {
       $FullFileName = realpath($dir.'/'.$file);
       if ((substr($FullFileName, 0, 1) != '.') && is_file($FullFileName)) {
         $i++;
         set_time_limit(30);
         $ThisFileInfo = $getID3->analyze($FullFileName);
         getid3_lib::CopyTagsToComments($ThisFileInfo);
         $xmlBody .= '<Song>';
         $xmlBody .= '<songNum>' . $i . '</songNum>
            <songURL>' .basename($ThisFileInfo['filenamepath']).'</songURL>
            <songArtist>' .(!empty($ThisFileInfo['comments_html']['artist']) ?  implode('<br>',$ThisFileInfo['comments_html']['artist'])  : $strIsNull) .'</songArtist>
            <songTitle>' .(!empty($ThisFileInfo['comments_html']['title']) ?implode('<br>', $ThisFileInfo['comments_html']['title'])  : $strIsNull) . '</songTitle>
            <songSize>'  .round($ThisFileInfo['audio']['bitrate'] / 1000).' kbps </songSize>
            <songDuration>' . $ThisFileInfo['playtime_string'].' </songDuration>';
          $xmlBody .= '</Song>';
       }
    }
    $xmlBody .= "</XML>";
    header('Content-type: text/xml');
    echo $xmlBody; 
Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
scottiescotsman
  • 73
  • 2
  • 12

1 Answers1

1

Try adding file_put_contents('playlist.m3u', $xmlBody); to the end of the file - this will save the contents of the playlist in playlist.m3u. You can use this in tandem with all the code you already have, or just disable/remove the echo if you don't need it.

As for your second question - in order to have the file update with the new folder contents, you will need to:

a) Have another process monitor for changes, then call this php script/file b) Setup a cron job on your server or c) Call the script manually from time to time (or from your flash player), by calling for e.g. http://localhost/my-m3u-generator.php

Community
  • 1
  • 1
dAngelov
  • 830
  • 6
  • 10
  • Added the extra code and removed the echo as you suggested, but ran the code and didn't create .m3u file. Haven't tried your second suggestion. – scottiescotsman Apr 30 '14 at 20:47
  • Perhaps you do not have permissions to save a new file in that folder. One way around it is to create the file `playlist.m3u` manually, once, change it's permissions so that the script can access it (you can use 0777 to try,before dabbling further into the permissions) - and then re-running the script. – dAngelov May 01 '14 at 11:02