I've been looking for some kind of tutorial, or some code but couldn't find much (that I understand!).

I have a table with an artist's name, song name, etc. I would like to display that artist's songs and then link those songs to an m3u file so it can stream. Right now I'm doing it with manually created m3u files but I would like to have it done by PHP.

For example, I would like the URL of the song name to be something like this: song.php?songid=xxx

Can someone point me in the right direction? Are there any tutorials on how to do this?

Btw, I'm not looking to create an m3u file that plays a playlist of mp3's, instead I want an m3u file for each mp3. I need to play each song separately, not a playlist.

Thanks in advance.

    // Get the data from db:
    $result = mysql_query("SELECT * FROM mp3_table");
    // Loop through the db results:
    while ($row = mysql_fetch_assoc($result)) {
        // Write the files:
        file_put_contents(basename($row['song'], '.mp3') . '.m3u', $row['song']);
    }

      Thanks for the reply.

      Looks like I'm on the right track with this code, however I'm trying to get the path to that folder first, then get the song name.

      I got this:

      $url = $_SERVER['DOCUMENT_ROOT'] . $dir;
      fopen($url . basename($row['song'], '.mp3') . '.m3u', $row['song']);
      

      But I get this error:

      Warning: fopen(/home/public_html/songs/songname.m3u): failed to open stream:

      I know the path is correct but for some reason it still won't open. Any ideas?

      *I'm using fopen because I'm not using PHP 5.

      Thanks.

        • fopen() doesn't take the same argument list as file_put_contents().
        • fopen() doesn't write to the file. You need to use fwrite() with it.

          Now I'm getting this:

          Warning: fwrite(): supplied argument is not a valid stream resource

          Any ideas? Thanks.

            This is all my code so far:

            $dir = "/path/to/directory";
            $url = fopen($dir, 'r');
            fwrite($url . basename($row['song'], '.mp3') . '.m3u', $row['song']);
            

              Follow the link I gave to fopen() and read. Then look at [edit: I mean read] the manual page for fwrite().

                I've been reading the manual for the past few hours and I'm still scratching my head on this one lol.

                See what I don't understand is I have $dir = /path/to/directory
                but "fopen" is to open a FILE, correct?

                  Correct. So which bit are you having the problem with? Trying to open a directory instead of a file? Opening it set to read instead of write? Using the resource returned from fopen() as if it was a string in write()? Trying to use fwrite() with a string instead of a resource? Trying to fwrite() to a file you never opened (at all, let alone opened for writing?)

                  It's hardly rocket science if you read the relevant pages Installer has cited. Open a file for writing, then write to it.

                    That's what I'm saying - it's supposed to be open a FILE for writing and then write to it. But in this case, what file am I opening? The mp3 file? Do you see something wrong with my code? The manual is very helpful but once I encounter an error that's when I'm having problems troubleshooting the code. 🙂

                      grantus wrote:

                      Do you see something wrong with my code?

                      Yes... the same thing the previous posts have said:

                      1. Your fopen() doesn't point to a file, but a directory. Try telling it what file to write to.

                      2. Your fopen() call is telling the system to open the file stream for reading only, not writing. Look in the manual for [man]fopen/man for the different modes to use... 'r' isn't going to allow you to write anything.

                      3. Your fwrite() call should have the file stream resource (returned from the fopen() call) as the first argument... but it does not. Once again, I'll point you to the manual for [man]fwrite/man for the coding examples.

                        Ok I managed to get the script working sort of. What I'm not clear about is that since I'm writing TO a file, how does the m3u creation work?

                        What I mean is, if I tell it what file to write to, in this case it's an mp3 file, I don't understand that part. What I'm trying to do is just create an m3u file for that particular mp3 file and then use that m3u file in a link. So why am I trying to write to an mp3 file?

                        Thanks.

                          grantus wrote:

                          since I'm writing TO a file, how does the m3u creation work?

                          How? Very easily.

                          $fp = fopen($url . basename($row['song'], '.mp3') . '.m3u', 'w');
                          
                          fwrite($fp, $row['song']);
                          
                          fclose($fp);

                          So... "how" does it work? Well, the 'w' mode for fopen() says: "If the file does not exist, attempt to create it. "

                            Ohhh ok. I was using 'a' instead of 'w'. My fault, I didn't see the full explanation.

                            The code works fine now, I'll tweak it a bit more to my liking, but it's writing fine.

                            Quick question though - every time the page loads it's going to be checking to see if the M3U file exists or not, does that cause any kind of slow down? Is it safe to do it like this?

                            Thanks.

                              It should slow things down no more, and probably less, than just rewriting the same files every time through. I see nothing unsafe about it.

                              I'm glad to see that Brad got you going on this. Use (even browse) the manual and you'll progress quickly.

                                I set it up some more so that I'm using file_exists to check to see if the file is there. Everything is working great now.

                                Thanks for all the help guys.

                                  Write a Reply...