PHP | How do I show elements of a directory and randomly order?

0

I'm trying to make a list of music, I have all the music in a folder and I want every time the screen refreshes, I get disordered, I already managed to call all the music in the folder with dir() , but what I do not get it is that every time the screen is refreshed I mess up the folder, I understand that it is done with shuffle() , but it is not working. I will leave you a part of my code:

<?php

$path="../mimusica"; //El directorio local donde almacena los archivos
$directorio= dir($path);

while ($archivo = $directorio->read();)
{

     $replace_mp3 = str_replace(".mp3","",$archivo);
     echo shuffle($replace_mp3)."<br>";

}
$directorio->close();

?>
    
asked by Juanse Portillo 05.12.2018 в 17:36
source

1 answer

1

The problem is that you are only ordering one file at a time. You have to store the files in an array and then order that array.

<?php

$path="../mimusica"; //El directorio local donde almacena los archivos
$directorio= dir($path);
$arrayFiles = array();

while ($archivo = $directorio->read();)
{

     $replace_mp3 = str_replace(".mp3","",$archivo);
     $arrayFiles[] = $replace_mp3;

}
$directorio->close();

shuffle($arrayFiles);

foreach($arrayFiles as $valor){
      echo $valor."<br/>";
}

?>
    
answered by 05.12.2018 / 17:45
source