List running in order with PHP

Asked

Viewed 269 times

1

i am working on a music playlist code with PHP, so I am 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 that? Follow 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>";

php list.

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

}

}

}
  • But you want me to start playing?

  • Instead of directly printing what the "readdir" returns, you should put and sort the contents in an Array in the way you think best. After that, just print.

2 answers

3


From what 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, please, instead of using the tag embed, use the tag audio:

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

thus, 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>
  • @Alexandre-Layonel Thank you very much :D

0

According to the documentation of the function readdir:

Returns the filename of the next directory file. The names of files are returned in the order informed by the file system. (http://php.net/manual/en/function.sort.php

You will get an ordered list according to the operating system. If you want a different order, I recommend that you send the directory files and add them in an Array. That 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 alphabetical order(you have not set), you should manually sort the Array as desired.

  • Thank you very much :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.