randomaydesigns Posted September 12, 2010 Share Posted September 12, 2010 Hey everyone, So, I'm designing a webapp that displays images that are uploaded by users. All of the images are uploaded to a folder on our server. I'd like to write a .php script that selects a random image from the folder. I'd also like the script to not reselect the same image until all other images have also been selected. For example, if there are 50 images. And image 3 is selected first, I don't want image 3 to be reselected until images 4-50 and images 1 and 2 are selected first. If that made any sense. Any help is appreciated. Thanks, George Link to comment https://forums.phpfreaks.com/topic/213186-image-randomizer/ Share on other sites More sharing options...
WatsonN Posted September 12, 2010 Share Posted September 12, 2010 <img src="/rotate/rotate.php" alt="A Random Image" /> rotate.php <?php // Make this the relative path to the images, like "../img" or "random/images/". // If the images are in the same directory, leave it blank. $folder = ''; // Space separated list of extensions, you probably won't have to change this. $exts = 'jpg jpeg png gif'; $files = array(); $i = -1; if ('' == $folder) $folder = './'; $handle = opendir($folder); $exts = explode(' ', $exts); while (false !== ($file = readdir($handle))) { foreach($exts as $ext) { if (preg_match('/\.'.$ext.'$/i', $file, $test)) { $files[] = $file; ++$i; } } } closedir($handle); mt_srand((double)microtime()*1000000); < 4.2 $rand = mt_rand(0, $i); // $i was incremented as we went along header('Location: '.$folder.$files[$rand]); ?> not sure if it only pulls once until all are displayed, pretty sure it doesn't. God place to add it in. . . -edit- Original script from http://ma.tt/scripts/randomimage/ -edit- Link to comment https://forums.phpfreaks.com/topic/213186-image-randomizer/#findComment-1110094 Share on other sites More sharing options...
BizLab Posted September 12, 2010 Share Posted September 12, 2010 You could also place the file names in a database table and call them using the ORDER BY rand() function.. Just a suggestion to better organize your code and file records.. This does not guarantee that each image will show only once in the rotation, but if you needed to guarantee that, you'd be better off making a slideshow. Link to comment https://forums.phpfreaks.com/topic/213186-image-randomizer/#findComment-1110111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.