jeudi 30 juillet 2015

Random image from directory with no repeats?

I am successfully able to get random images from my 'uploads' directory with my code but the issue is that it has multiple images repeat. I will reload the page and the same image will show 2 - 15 times without changing. I thought about setting a cookie for the previous image but the execution of how to do this is frying my brain. I'll post what I have here, any help would be great.

$files = glob($dir . '/*.*');
$file = array_rand($files);
$filename = $files[$file];
$search = array_search($_COOKIE['prev'], $files);

if ($_COOKIE['prev'] == $filename) {
    unset($files[$search]);
    $filename = $files[$file];
    setcookie('prev', $filename);
}




Aucun commentaire:

Enregistrer un commentaire