...

View Full Version : PHP/XML question



wayne3503
08-08-2007, 07:51 PM
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

mlseim
08-08-2007, 08:06 PM
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?

CFMaBiSmAd
08-08-2007, 08:13 PM
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.

wayne3503
08-08-2007, 08:51 PM
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.

mlseim
08-09-2007, 12:31 AM
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.

wayne3503
08-09-2007, 01:54 AM
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.

<?
//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);
?>

//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?

usik
08-09-2007, 02:19 AM
no no he wants to upload a song than it automatically adds it to the playlist, here is an example i did


<?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
$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



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


or



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

wayne3503
08-09-2007, 03:51 AM
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...

$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

$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?

StupidRalph
08-09-2007, 06:07 AM
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.

usik
08-09-2007, 06:24 AM
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
$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($file, filesize($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>", $contents, 2);

//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.


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 :thumbsup: 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
$mp3s = array (
0 => 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($file, filesize($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>", $contents, 2);

//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!";
?>

StupidRalph
08-09-2007, 08:16 AM
Looks perfectly suitable to me. :thumbsup: But, I would've let thread starter give it a whack before I offered any code. But thats just my take on it.

wayne3503
08-09-2007, 03:27 PM
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.:D

wayne3503
08-09-2007, 03:59 PM
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

mlseim
08-09-2007, 07:13 PM
Wayne ... Look at some of these ...

http://www.google.com/search?hl=en&safe=off&q=php+read+id3&btnG=Search

I think you'll be looking for the ID3

wayne3503
08-09-2007, 07:43 PM
wow thanks alot. i may have to do some playing around this weekend and see what i can whip up.
Thanks again



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum