Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    108
    Thanks
    0
    Thanked 2 Times in 2 Posts

    PHP/XML question

    Ok I have an mp3 player for my site. Now I still have to make an upload script but I am wondering something. How would I go about making the scritp write the needed information to the playlist.xml file?
    So for instance a member uploads a file called "Cowboys_from_hell.mp3" how would I insert this info to the play list?
    <track>
    <title>Cowboys From Hell</title>
    <creator>Pantera</creator>
    <location>cowboys_from_hell.mp3</location>
    </track>
    The tags are already in the playlist so I just need to insert the data between the tags.

    I have never done anything with xml or saving info to files so this is new to me. Maybe this is even a db thing to, i dont know. if it helps any i am using the jw_mp3_player.

    Thanks for all the help

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,378
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    I'm a little confused about this one ...

    With the program as it is now, don't you upload the MP3 music file into
    a directory, and the Flash Player creates the playlist for you?

    With that, I mean the Flash Player reads the MP3 information from the MP3
    file itself ... title, creator, etc.

    So, if you only had a program that uploads the MP3 file, that's all you would need. Isn't that the way it works?

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    XML is not a database. It is a universal format for describing data so that it may be shared - http://en.wikipedia.org/wiki/XML

    If you want to store data, use a database.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    108
    Thanks
    0
    Thanked 2 Times in 2 Posts
    mlseim: the program I have is just a player, there is no upload script and therefor I will have to create one. So since I will have to create the upload script then i will have to find a way to write the needed information to the playlist.xml file for the files to show on the players list. Also with the program I have it reads the needed info from the xml file as I found out playing around with it.

    CFMaBiSmAd: I know its not a db, i just didnt know if I would need to use a db in addition to this. But I can clearly see that i wont need to now.

    Would you guys like me to attach the program to a message so you can see it and possibly understand what I am trying to do?

    thank you guys.

  • #5
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,378
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    I looked at the flash player website and don't see anything about
    manually creating the playlist.xml file. Maybe I missed that part?

    Anyhow, maybe it's best to use a simple flat-file text file to keep
    your list, and then use PHP to create the "playlist.xml" file from that.

    You would trigger the PHP script only when you need to re-generate
    the XML file ... so it's cached and not dynamic.

    Look at some tutorials on using a flat-file database...

    Example:

    Back In Black|AC-DC|1980|blah|blah|blah|
    Highway To Hell|AC-DC|1978|blah|blah|blah|

    Each field separated by pipes |

    You'll have to search Google for tutorials.

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Posts
    108
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Well here is a little script i found..I have made alterations and trying to fiure some of it out yet but you should get the idea.
    PHP Code:
    <?
    //http://www.developerfusion.co.uk/show/3944/
    $doc new_xmldoc('1.0');
    $root $doc->add_root('tracklist');
    $member $root->new_child('track','');

    $member->new_child('title','song');
    $member->new_child('creator','band');
    $member->new_child('location','path');

    $member $root->new_child('track','');

    $member->new_child('title','song');
    $member->new_child('creator','band');
    $member->new_child('location','path');

    $member $root->new_child('track','');

    $member->new_child('title','song');
    $member->new_child('creator','band');
    $member->new_child('location','path');

    $fp = @fopen('playlist.xml','w');
    if(!
    $fp) {
        die(
    'Error cannot create XML file');
    }
    fwrite($fp,$doc->dumpmem());
    fclose($fp);
    ?>
    PHP Code:
    //creates the below xml
    <tracklist>
      <
    track>
        <
    title>One</title>
        <
    creator>Metallica</creator>
        <
    location>music/one.mp3</location>
      </
    track>
      <
    track>
        <
    title>Hells Bells</title>
        <
    creator>ACDC</creator>
        <
    location>music/hell_bells.mp3</location>
      </
    track>
      <
    track>
        <
    title>Cowboys_from_hell</title>
        <
    creator>Pantera</creator>
        <
    location>music/cowboys_from_hell.mp3</location>
      </
    track>
    </
    tracklist
    Now Im thinking this should work for the most part. What I will need to figure out now is...
    1) how to get this scriptt to trigger when a file gets uploaded
    and
    2) Is there a way to grab band name and song title from the mp3 it self or will I have to have that data be put in manually by the site member?

  • #7
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    no no he wants to upload a song than it automatically adds it to the playlist, here is an example i did
    Code:
    <?php
    $content_you_want_to_add = "<track>\n<title>Cowboys From Hell</title>\n<creator>Pantera</creator>\n<location>cowboys_from_hell.mp3</location>\n</track>\n";
    
    //pathname to file
    $filename = "c:/playlist.xml";
    
    //opens file readonly
    $file = fopen($filename, "r") or die("Cannot open file");
    
    //gets all the content from the file
    $contents = fread($file, filesize($filename)) or die("Error reading from file");
    fclose($file);
    
    //creates the string with the original contents plus the contents you want to add
    $newcontent = $contents . $content_you_want_to_add;
    
    //reopens file writeonly and truncates the file clean
    $file = fopen($filename, "w") or die("Cannot reopen file");
    
    //writes all the original content back into the file plus the new track
    fwrite($file, $newcontent) or die("Error writing to file");
    fclose($file);
    echo "Adding song to $filename was a success!";
    ?>
    that is a windows example but you can change the path to unix, but
    Code:
    $contents = fread($file, filesize($filename)) or die("Error reading from file");
    will only work for local machine files, if you use a url you will need to replace the code with

    Code:
    // For PHP 5 and up
    $contents = stream_get_contents($file);
    or

    Code:
    $contents = '';
    while (!feof($handle)) {
      $contents .= fread($file, 8192);
    }

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Posts
    108
    Thanks
    0
    Thanked 2 Times in 2 Posts
    So ok I have a copy of all the code saved. Just to make sure im on the right page with this, Im going to make a form that just allows the user to browse for file and upload it. I then have this code as an include in my form and this will then read the mp3 and enter in the needed info into the xml file. Do I have that all correct? The only thing I can see that I would need to do is edit this...
    PHP Code:
    $content_you_want_to_add "<track>\n<title>Cowboys From Hell</title>\n<creator>Pantera</creator>\n
    <location>cowboys_from_hell.mp3</location>\n</track>\n"

    Into something like this
    PHP Code:
    $content_you_want_to_add "<track>\n<title>$title</title>\n<creator>$band</creator>\n
    <location>$path</location>\n</track>\n"

    wouldnt I need something like that so that way multiple songs could be uploaded and the playlist would account for them? or would all this info be grabed from the mp3 itself?

  • #9
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    If you append it to the end of your file then its going to make your xml invalid as your tags will be inappropriately nested. (the newest added track will be beneath the root tag.) You would have to first strip off the root tag and then add the track and then add it back. I didn't do this for you as its simple enough to figure out how.

    I personally would use a database and a php file which poses as an XML file to flash.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #10
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post

    Thumbs up

    Quote Originally Posted by StupidRalph View Post
    If you append it to the end of your file then its going to make your xml invalid as your tags will be inappropriately nested. (the newest added track will be beneath the root tag.) You would have to first strip off the root tag and then add the track and then add it back. I didn't do this for you as its simple enough to figure out how.

    I personally would use a database and a php file which poses as an XML file to flash.
    well if that is the case you can do the following with the content of the xml file

    PHP Code:
    <?php
    $content_you_want_to_add 
    "<track>\n<title>Cowboys From Hell</title>\n<creator>Pantera</creator>\n<location>cowboys_from_hell.mp3</location>\n</track>\n</xml>";

    //pathname to file
    $filename "c:/playlist.xml";

    //opens file readonly
    $file fopen($filename"r") or die("Cannot open file");

    //gets all the content from the file
    $contents fread($filefilesize($filename)) or die("Error reading from file");
    fclose($file);

    //removes the </xml> tag from the file to allow to to add new content
    list($content) = split("</xml>"$contents2);

    //creates the string with the original contents plus the contents you want to add
    $newcontent $content $content_you_want_to_add;

    //reopens file writeonly and truncates the file clean
    $file fopen($filename"w") or die("Cannot reopen file");

    //writes all the original content back into the file plus the new track
    fwrite($file$newcontent) or die("Error writing to file");
    fclose($file);
    echo 
    "Adding song to $filename was a success!";
    ?>
    take the end of the root tag off from the content than put it back on with the new added content.

    Quote Originally Posted by wayne3503
    So ok I have a copy of all the code saved. Just to make sure im on the right page with this, Im going to make a form that just allows the user to browse for file and upload it. I then have this code as an include in my form and this will then read the mp3 and enter in the needed info into the xml file. Do I have that all correct? The only thing I can see that I would need to do is edit this...
    Yes exactly and to add more than 1 song you would need to do a loop of sorts, perhaps a while statement or save all of the songs into an array and do a foreach?

    PHP Code:
    <?php
    $mp3s 
    = array (
                 
    => array (
                          
    "Name" => "Cowboys From Hell",
                          
    "Artist" => "Pantera",
                          
    "Location" => "cowboys_from_hell.mp3"
                         
    )
                 );

    foreach(
    $mp3s as $mp3) {
    $content_you_want_to_add $content_you_want_to_add "<track>\n<title>{$mp3["Name"]}</title>\n<creator>{$mp3["Artist"]}</creator>\n<location>{$mp3["Location"]}</location>\n</track>\n";
    }

    $content_you_want_to_add $content_you_want_to_add "</xml>";

    //pathname to file
    $filename "c:/playlist.xml";

    //opens file readonly
    $file fopen($filename"r") or die("Cannot open file");

    //gets all the content from the file
    $contents fread($filefilesize($filename)) or die("Error reading from file");
    fclose($file);

    //removes the </xml> tag from the file to allow to to add new content
    list($content) = split("</xml>"$contents2);

    //creates the string with the original contents plus the contents you want to add
    $newcontent $content $content_you_want_to_add;

    //reopens file writeonly and truncates the file clean
    $file fopen($filename"w") or die("Cannot reopen file");

    //writes all the original content back into the file plus the new track
    fwrite($file$newcontent) or die("Error writing to file");
    fclose($file);
    echo 
    "Adding song to $filename was a success!";
    ?>
    Last edited by usik; 08-09-2007 at 06:44 AM.

  • #11
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Looks perfectly suitable to me. But, I would've let thread starter give it a whack before I offered any code. But thats just my take on it.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #12
    Regular Coder
    Join Date
    Jul 2007
    Posts
    108
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Yea I think ill need to do a loop of sorts. because if Im not mistaken the code you have above will use these paramiters for every file uploaded
    "Name" => "Cowboys From Hell",
    "Artist" => "Pantera",
    "Location" => "cowboys_from_hell.mp3"
    ...and all files will end up having the same name, artist, and location no matter what was uploaded, wouldnt they? I know I must have some loop code in one of my scripts so ill see where I get with that.

  • #13
    Regular Coder
    Join Date
    Jul 2007
    Posts
    108
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I think I found something that works. Ill just have to customize a bit but it works pretty slick. Ill keep you all posted incase any of you are interested. by the way, is it possible with php to actually grab info such as band and song name from the MP3 file itself?

    Thank you all for the help

  • #14
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,378
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Wayne ... Look at some of these ...

    http://www.google.com/search?hl=en&s...d3&btnG=Search

    I think you'll be looking for the ID3

  • #15
    Regular Coder
    Join Date
    Jul 2007
    Posts
    108
    Thanks
    0
    Thanked 2 Times in 2 Posts
    wow thanks alot. i may have to do some playing around this weekend and see what i can whip up.
    Thanks again


  •  

    Posting Permissions

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