List execution neatly with PHP

1

I'm working on a music playlist code with PHP, so I'm putting the command to play the song by the $ filename variable:

echo "<br><embed src=\"$folder/$filename\" widht=1 height=50><br>";

But when I run this command, it plays all the songs in the folder and not one by one (neatly) as I would like. How can I do this? Here are the complete codes:

index.php

<?php
header('Content-type: text/html; charset=ISO-8859-1');
echo '<form action="list.php">';
echo "<h4>Write your songs folder</h4><br>";
echo "<h5>(DON'T WRITE A BAR IN THE END OR WRITE ANY SPECIAL CHARACTER)</h5>";
echo '<input type="text" name="folder">';
echo '<input type="submit">';
echo "</form>";

list.php

<?php
header('Content-type: text/html; charset=ISO-8859-1');

$folder = $_GET["folder"];
$files  = "{$folder}/".$_GET['/*.*'];

if(isset($_GET['folder']) && file_exists("{$folder}/".$_GET['/*.*'])){

$fold = opendir($folder);

    while (false !== ($filename = readdir($fold))) { 

        if (substr($filename,-4) == ".mp3") { 
        echo "<br><embed src=\"$folder/$filename\" widht=1 height=50><br>";

}

}

}
    
asked by anonymous 18.12.2016 / 01:12

2 answers

3

As far as I understand, your problem has more to do with JavaScript than with PHP. You want the songs to be played one after the other, right?

So, instead of using the embed tag, please use the audio tag. :

<audio controls>
  <source src="pasta/arquivo.mp3" type="audio/mp3">
</audio>

So, in PHP:

while (false !== ($filename = readdir($fold))) { 
    if (substr($filename,-4) == ".mp3") { 
        echo "<audio controls><source src=\"$folder/$filename\" type=\"audio/mp3\"></audio><br>";
    }
}

For each song to play after the end of its predecessor, use javascript:

<script>
    function play_next(event) {
        var next = event.target.nextElementSibling.nextElementSibling
        if (next)// se existir uma próxima música
           next.play();// a toca
    }
    var musicas = document.getElementsByTagName('audio'); // seleciona todas as musicas
    var len = musicas.length;
    for (var i = 0; i < len; i++)
        musicas[i].onended=play_next; // quando essa música acabar, chama play_next
    musicas[0].play(); // toca a primeira
</script>
    
18.12.2016 / 04:51
0

According to the documentation of the readdir function:

  

Returns the file name of the next file in the directory. The names of   files are returned in the order reported by the file system.   ( link

You will get an ordered list according to the operating system. If you want a different order, I recommend you to iterate the files in the directory and add them to an Array. This way:

$files = [];
if(isset($_GET['folder']) && file_exists("{$folder}/".$_GET['/*.*'])){
$fold = opendir($folder);
      while (false !== ($filename = readdir($fold))) { 

        if (substr($filename,-4) == ".mp3") { 
        $files[] = $filename;

      }
   }
}

Then you can use the PHP sort function if you want alphabetical order.

$files = sort($files,SORT_STRING); 

And then you print the tags:

foreach($files as $file) {

      echo "<br><embed src=\"$folder/$file\" widht=1 height=50><br>";
}

If it is not an alphabetic order (you have not defined it), you must sort the Array manually according to the desired criteria.

    
18.12.2016 / 04:14