-4

I'm trying to have a page select a song at random from a dir of .mp3 files and play it. This is what I have so far http://pastebin.com/Jypx80RD I get this error:

Second argument has to be between 1 and the number of elements in the array

For this line: $random = array_rand($files, 2);

Sharkie
  • 51
  • 1
  • 4
  • As the error message explains, your second argument of `array_rand` isn't valid, because it isn't within the legal range. Have you tried fixing this? If so, what is the problem you are encountering? – FThompson Mar 23 '14 at 21:58
  • Possible duplicate of [Select random file using OPENDIR()](http://stackoverflow.com/questions/12119304/select-random-file-using-opendir) – SpYk3HH Jul 14 '16 at 15:29
  • http://stackoverflow.com/questions/12119304/select-random-file-using-opendir/38361953#38361953 – SpYk3HH Jul 14 '16 at 15:29

2 Answers2

1

Try this:

$random = $files[rand(0, count($files) - 1)];
Lee Salminen
  • 900
  • 8
  • 18
0

This should do it:

$random = array_rand($files);

$files looks like it may be empty, from what I had to guess though.

Mike Weir
  • 3,094
  • 1
  • 30
  • 46