Move Randomly Chosen File

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!

If you can display it, then you can hold onto that same info for moving it. Though I don’t understand why you would want to do this.

I agree with Astonecipher!

If you have a folder with GIF’s in it, you can read them all with simple code and have a list of them.
You could save that list in a DB table and order them somehow or mark/flag them displayed.

Also, if you show a GIF for one user, the next user would not see it as you would have moved it away.
None of all this processing makes much sense. What are you attempting to do? If the site is just for
you and you only, then you could handle it this way. Please give us some further info…

Yeah, the site is for only one person, a coworker of mine. They’re sent there after submitting a form, and it displays a message saying thanks and a random gif. The site is primarily a way for me to practice coding different things. I suppose this would have been helpful info to have included at the start. . . .

I want to move the displayed gifs to a seperate folder mainly to simplify the task of periodically deleting them. If I just marked the displayed files as displayed and kept them in the same folder, I suspect I would be more likely to just leave them there to pile up.

Thanks for taking the time to respond! I really appreciate your input.

In that case, rather than moving them, why not just delete them to begin with?

I considered that, but I couldn’t figure out how to write the code such that it would delete the file only after it had displayed it. I still have trouble with the order of operations of things when coding. I can’t wrap my head around how the code would detect that the image no longer needs to be displayed and can be deleted.

Not a big deal then.

So, do you have it worked out yet?

I have not. I tried this:

<?php

$root = $_SERVER['DOCUMENT_ROOT'];
$path = '/testGifs/'; // copy of folder with gifs
$gif = '/usedGifs/displayed.gif';

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 ); // php 4.2+ not needed
    $num = array_rand($ar);
    return $ar[$num];
}

// Obtain list of images from directory 
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
$preGif = $path . $img;
// Move gif to new directory
rename($preGif,$gif);
?>

But I got this error: Warning: rename(/testGifs/mouse.gif,/usedGifs/displayed.gif): No such file or directory in /…/file.php on line 31.

break the functionality down more. You need an overall function to call to get the random image.

function getRandomImage()
{
    // get the images first
    $images = getImagesFromDir($path);
    // now pick the random
    $imageToDisplay = getRandomFromArray($images);
    // move it and get the path back to where it is
    $randImage = moveImageToUsedDir($image);
    return is_null($randImage) ? null : $randImage;
}

This is a template pattern. It allows you to keep everything organized in the order you want, but also keep the single responsibility function of each thing it needs to do.

The last thing (moveImageToUsedDir) should return the images full path to the used directory with the image on the end; ie, “/usedFiles/someimage.gif”

You can use that for the display, since it has already been moved, and it is out of circulation for the next round of selection.

Sponsor our Newsletter | Privacy Policy | Terms of Service