Search code examples
phphtmlmp3playlist

Files in folder displayed with PHP automatically inputted into php/html playlist


I have a what seems like simple issue. I would like to have my playlists auto populated with the files in a folder. So as mp3s are added to the folder the files are generated in the playlist. I can display all the files in a folder and have the file names outputted on there own line using:

<?php
$dir = "app/vd/$val/";
$files = scandir($dir);
foreach ($files as &$file) {
    if ($file!='.' && $file!='..' )
    {
        echo $file.'<br>';
    }
}
?>

And I manually add files to the play list using:

<ul id="playlist" style="margin-left:auto; margin-right:auto;">
<li mp3="app/vd/<?php echo $val ?>/whatever.mp3" ogg="app/vd/<?php echo $val ?>/whatever.ogg" artist="<?php echo $val ?>" title="<?php echo $val1 ?>" ></li>

<li mp3="app/vd/<?php echo $val ?>/whatever2.mp3" ogg="app/vd/<?php echo $val ?>/whatever2.ogg" artist="<?php echo $val ?>" title="<?php echo $val1 ?>" ></li> 

Any help would be greatly appreciated.


Solution

  • Just put it between the ul tags ...

    <ul id="playlist" style="margin-left:auto; margin-right:auto;">
        <?php
        $dir = "app/vd/$val/";
        $files = scandir( $dir );
        foreach ( $files as $file )
            if ( $file != '.' && $file != '..' )
                echo '<li mp3="app/vd/', $val, '/', $file,'" ogg="app/vd/', $val, '/', $file, '" artist="', $val, '" title="', $val1, '" ></li>';
        ?>
    </ul>
    

    or to make it look nicer

    <?php $files = scandir( "app/vd/$val/" ); ?>
    <ul id="playlist" style="margin-left:auto; margin-right:auto;">  
        <?php foreach ( $files as $file )
            if ( $file != '.' && $file != '..' )
                echo "<li mp3=\"app/vd/$val/$file\" ogg=\"app/vd/$val/$file\" artist=\"$val\" title=\"$val1\" ></li>"; ?>
    </ul>
    

    P.S.: glob is a neat little function http://de3.php.net/manual/en/function.glob.php P.P.S: iterators on directories are neat, too