Hi everyone,

I'm hoping someone can help me here!

What I'm trying to do:
Loop through an uploaded file ( .m3u - playlist from Winamp) and add the contents to a database.

I'm having trouble parsing the .m3u file however. In the following code snippet it tries to display to the browser. If I can get it to display to the browser, I figure it will be easy to write it to the database!?

Here is my code snippet:

<?
// Convert seconds to minutes m:ss
function TimeFromSec($sec)
{
 $minutes = floor(($sec % 3600) / 60);
 $seconds = floor(($sec % 3600) % 60);
 if(strlen($seconds) == 1)
  {
   $seconds = "0".$seconds;
  }
 return $minutes.":".$seconds;
}

// Open the file
$fd = fopen($_FILES['userfile']['name'], "r");

// Display the top of the table
?>
<table border="0" cellpadding="0" cellspacing="0" bgcolor="#CCFFFF" width='500'>
  <tr>
<td bgcolor="#000000"><font color="#99FFFF" face="Arial" size="-1">Artist</font></td>
<td bgcolor="#000000"><font color="#99FFFF" face="Arial" size="-1">Title</font></td>
<td bgcolor="#000000"><font color="#99FFFF" face="Arial" size="-1">Time</font></td>
</tr> 
<?
//read the file line by line...
while (!feof($fd))
   { 
   $buffer = fgets($fd);
//and parse it line by line...     
if(ereg('#EXTINF:\([[:digit:]]+),([A-Z\' &^`~!@$%*,_]+) - ([A-Z\' &^`~!@$%*,_-]+)', $buffer, $reg)) { //output the result echo '<tr><td style="border-bottom:1px solid black; border-left:1px solid black"> <font face="Arial" size="-1">'.$reg[2].'&nbsp;</td><td style="border-bottom:1px solid black"><font face="Arial" size="-1">'.$reg[3].'</td><td align = "center" style="border-bottom:1px solid black; border-right:1px solid black"> <font face="Arial" size="-1">'.TimeFromSec($reg[1]).'</td></tr>'; } } //Close the file fclose ($fd); //Delete the file unlink($realname); ?> </table>

For reference, an .M3U file looks like this:

#EXTM3U
#EXTINF:272,Unknown Artist - Track 1
F:\Track01.cda
#EXTINF:252,Unknown Artist - Track 2
F:\Track02.cda
#EXTINF:237,Unknown Artist - Track 3
F:\Track03.cda

Any help, suggestions, etc would be greatly appreciated!

Thank you,
Dan

    $buffer = "";
    while (!feof($fd)) 
       {  
    $buffer .= fgets($fd) . "\n"; } $q = "insert into m3utable set m3u_info = '$buffer'";

    -=Lazzerous=-

      Thanks Lazzerous!

      It now gets to a point when it parses the file line by line!

      My code now looks like this:

      // Clear the buffer
         $buffer = "";
      
      // Parse the file line by line...
        while (!feof($fd))
      	  { 
      	  $buffer .= fgets($fd) . "\n";
      	}
      
      // Just to see it work
           echo $buffer;
      

      How do I break it up so that I can put the individual items in the database? I need to log the ID, Artist, Song, and directory seperately - for each line, for each database entry.

      Sorry, I'm a little new to PHP :/

      Any help would be greatly appreciated!

      Thanks much,
      Dan

        Can someone spare the time it would take to answer my question?! The reply doesn't need to be with code, just point in the right direction please!

        Thank you for your time,
        Azroth

          Write a Reply...