I have some code that chooses a random gif file from a directory (/freshGifs/) to display on a website. I want to move the chosen gif to a different directory (/usedGifs/) after it is chosen but before it is displayed so that it will not get chosen to be displayed again. However, I can’t figure out how to move the file since it and its path are randomly generated, and as far as I understand it, rename requires a specified path to move a file.
Here’s the current code:
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
$path = '/gifs/';
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// check for gif
if ( preg_match("/(\.gif)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($ar);
return $ar[$num];
}
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
Thanks for any help you can provide!