List files in a directory with a checkbox. Then write 2 xml?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crewxp
Forum Newbie
Posts: 4
Joined: Sun Feb 19, 2006 10:22 pm

List files in a directory with a checkbox. Then write 2 xml?

Post by crewxp »

Okay, I'm really really sorry if this seems hard to do, but from what I've searched for, I've seen people easily come up with this, but I can't figure out how to combine it all. I'm really new to php coding. And I wanted to get a website I was doing for a school up in a few days.

Anyways, I made a XML flash player out of macromedia. Took me a while, but I did it. And I'm trying to do this.

Have a php script that reads all of the files in the directory (ex: files), and shows them in a list with a checkbox to the left of them (They're mp3's wma's etc).

Then I can select each listing (mp3 file) I want, then press a button below (Generate), and it creates an xml file for my mp3 player with the files it found.

My XML file is like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

<player showDisplay="yes" showPlaylist="yes" autoStart="yes"
>
  <song path="http://www.bcomedia.com/files/Holland%20Residence.mp3" title="Holland Residence" />
  <song path="http://www.bcomedia.com/files/From%20Concentrate.mp3" title="From Concentrate" />
  <song path="http://www.bcomedia.com/files/Art%20Of%20Life.mp3" title="Art of Life" />
[/SIZE]

Where Holland Residence.mp3 is the file it found in the files folder. And then it strips off .mp3 and adds that to title.

Directory Structure Example:
script.php (Root Directory)
mp3player.swf (Root Directory)
xmlfile.xml (Root Directory)
-files
--Holland Residence.mp3
--From Concentrate.mp3
--Art Of Life.mp3

Thanks so much!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what do you need guidance on?

opendir()/readdir(), glob() can be used to read the files in a directory.
substr()-strpos(), preg_match() can be used to extract the "name" or you could use a :arrow:class to read the tag information in the files.
crewxp
Forum Newbie
Posts: 4
Joined: Sun Feb 19, 2006 10:22 pm

Post by crewxp »

I kind of know how to read a folder then display what it finds.

But I don't know how to make it write an xml file based on what's checked.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fopen()-fwrite()-fclose() is the formula. :)
crewxp
Forum Newbie
Posts: 4
Joined: Sun Feb 19, 2006 10:22 pm

Post by crewxp »

Thanks for those links. I get how to open it, write to it, close it now.

But I don't know how the checkboxes work still. How can I choose what to add and delete to the file?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

foreach(glob('*.mp3') as $file) {
   echo $file.'<input type="checkbox" name="'.$file.'" value="1" />';
}
???
crewxp
Forum Newbie
Posts: 4
Joined: Sun Feb 19, 2006 10:22 pm

Post by crewxp »

I tried something... I got it to read the directory, show them in a list with a checkbox.

But... I can't get it to work right. When it echo's it at the end, it just shows /n for each box I've checked.
And I don't know how to write the xml either..

Code: Select all

<?PHP

$buildhtml = "";
$dirpath = "crewxp/music/guitar"; 
    $dlist = opendir($dirpath); 
       while ($file = readdir($dlist)) { 
           if (!is_dir("$dirpath/$file")) { 
               $buildhtml .= "<input type=checkbox name=checkfiles[] value=$file>$file<BR>"; 
           } 
       } 
     closedir($dlist); 

if (!empty($buildhtml)) {
    echo "<form action=\"test.php\" method=post>Choose your files:<p>" . $buildhtml . "</p>";
    echo "<input type=submit></form>";
} else {
    echo "The directory is empty";
}

?>
<?PHP
$file="";
$buildxml = "";
if (isset($_POST['checkfiles']) && is_array($_POST['checkfiles'] )) {
     foreach($_POST['checkfiles'] as $file) {
          $buildxml .= "<song path=\"" . $web_path . $file . "\" title=\"" . substr($file, 0, -4) . "\" />/n";
     }
}

$finalxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$finalxml .= "<player showDisplay=\"yes\" showPlaylist=\"yes\" autoStart=\"yes\">";
$finalxml .= $buildxml;


echo "$finalxml";
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

\n not /n
Post Reply