Page 1 of 1

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

Posted: Sun Feb 19, 2006 10:23 pm
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!!!

Posted: Sun Feb 19, 2006 10:38 pm
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.

Posted: Sun Feb 19, 2006 10:43 pm
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.

Posted: Sun Feb 19, 2006 10:46 pm
by feyd
fopen()-fwrite()-fclose() is the formula. :)

Posted: Sun Feb 19, 2006 11:13 pm
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?

Posted: Sun Feb 19, 2006 11:42 pm
by josh

Code: Select all

foreach(glob('*.mp3') as $file) {
   echo $file.'<input type="checkbox" name="'.$file.'" value="1" />';
}
???

Posted: Mon Feb 20, 2006 10:48 am
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";
?>

Posted: Mon Feb 20, 2006 10:58 am
by feyd
\n not /n